[英]Google contacts API problems from JavaScript client with OAuth2 authentication
经过数小时的努力,Docs似乎很糟糕。 基本上我试图让读访问OAuth2用户身份验证的用户接触,即使用便携式通讯录API或完全成熟的联系人API 。 Google 最近开始允许OAuth2 。
通过首先使用户通过范围“ https://www.google.com/m8/feeds”进行身份验证,我可以通过Contacts API访问用户联系人。 然后,我可以使用jQuery检索他们的前25个联系人(显示的代码为CoffeeScript )
$.ajax
url: "https://www.google.com/m8/feeds/contacts/default/full"
dataType: 'jsonp'
data: { access_token: token, alt: 'json-in-script' }
success: (data, status) ->
console.log "The returned data", data
那行得通,我得到了JSON数据。 但是,几乎令人难以置信的是,Google提供的唯一联系订单(据我所知)是“ lastmodified ”(严重是wtf?)。 我需要更多类似“顶级朋友”或“最受欢迎”的东西。
恰好是Google Portable Contacts API 可以执行的操作 ,(是!)。 当然,我似乎无法成功申请工作。
首先,通过单击此链接,使用户通过便携式联系人API进行身份验证(注意范围:“ https://www-opensocial.googleusercontent.com/api/people”)
<a href="https://accounts.google.com/o/oauth2/authclient_id=457681297736.apps.googleusercontent.com&response_type=token&redirect_uri=http://localhost:3000/team&scope=https://www-opensocial.googleusercontent.com/api/people">Import Google Contacts</a>
效果很好,我获得了回传的访问令牌。
接下来,我尝试将ajax请求发送到便携式联系人API
$.ajax
url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all"
dataType: 'jsonp'
data: { access_token: token, alt: 'json-in-script' }
success: (data, status) ->
console.log "The returned data", data
但这会返回403错误
403 (The currently logged in user and/or the gadget requesting data, does not have access to people data.
有什么想法我做错了吗?
附录
我在Google OAuth2论坛中找到了此错误报告 ,该报告建议我们在使用Portable Contacts API时需要设置授权标头。 所以我这样尝试:
$.ajax
url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all"
dataType: 'jsonp'
data: { access_token: token, alt: 'json-in-script' }
beforeSend: (xhr) ->
xhr.setRequestHeader "Authorization", "OAuth #{token}"
data: { access_token: token }
success: (data, status) ->
console.log "The returned data", data
但这会让我遇到相同的403错误:
403 (The currently logged in user and/or the gadget requesting data, does not have access to people data
问题是您显然无法在JSONP请求上设置请求标头。 有关更多信息,请参见此问题的答案。
据我所知,替代方案是:
Google应该不难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.