[英]Google contacts API problems from JavaScript client with OAuth2 authentication
Been wrestling with this for many hours now, the Docs seem to be terrible. 经过数小时的努力,Docs似乎很糟糕。 Basically I'm trying to get read access to an OAuth2 authenticated users contacts, using either the Portable Contacts API or the full blown Contacts API .
基本上我试图让读访问OAuth2用户身份验证的用户接触,即使用便携式通讯录API或完全成熟的联系人API 。 Google have recently started allowing OAuth2 .
Google 最近开始允许OAuth2 。
I can get access to a users contacts via the Contacts API by first getting the user to authenticate with the scope: "https://www.google.com/m8/feeds". 通过首先使用户通过范围“ https://www.google.com/m8/feeds”进行身份验证,我可以通过Contacts API访问用户联系人。 Then I can retrieve their first 25 contacts using jQuery (code shown is CoffeeScript )
然后,我可以使用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
That works, and I get JSON data. 那行得通,我得到了JSON数据。 However, almost unbelievably, the only contacts order that Google provides (as far as I can tell) is ' lastmodified ' (seriously wtf?).
但是,几乎令人难以置信的是,Google提供的唯一联系订单(据我所知)是“ lastmodified ”(严重是wtf?)。 I need something more like 'top friends' or 'most popular'.
我需要更多类似“顶级朋友”或“最受欢迎”的东西。
Which, happens to be something that the Google Portable Contacts API can do , (Yay!). 恰好是Google Portable Contacts API 可以执行的操作 ,(是!)。 Of course, I can't seem to get a successful request to work.
当然,我似乎无法成功申请工作。
First, I get the user to authenticate with the portable contacts API by clicking this link (note the scope: "https://www-opensocial.googleusercontent.com/api/people") 首先,通过单击此链接,使用户通过便携式联系人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>
That works fine, and I get an access token passed back. 效果很好,我获得了回传的访问令牌。
Next I try to send an ajax request to the portable contacts API 接下来,我尝试将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
But that returns a 403 Error 但这会返回403错误
403 (The currently logged in user and/or the gadget requesting data, does not have access to people data.
Any ideas what I'm doing wrong? 有什么想法我做错了吗?
Appendix 附录
I found this bug report in the Google OAuth2 forum which advised that we need to set an authorization header when working with the Portable Contacts API. 我在Google OAuth2论坛中找到了此错误报告 ,该报告建议我们在使用Portable Contacts API时需要设置授权标头。 So I tried that like this:
所以我这样尝试:
$.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
But that gets me the same 403 error: 但这会让我遇到相同的403错误:
403 (The currently logged in user and/or the gadget requesting data, does not have access to people data
The problem is that you apparently can't set a request header on a JSONP request. 问题是您显然无法在JSONP请求上设置请求标头。 See the answer on this question for more information.
有关更多信息,请参见此问题的答案。
The alternatives as far as I can see are: 据我所知,替代方案是:
It shouldn't be this difficult Google. Google应该不难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.