Dav*_*ite 7 javascript google-contacts-api oauth-2.0
几个小时以来一直在与这个摔跤,Docs似乎很糟糕.基本上我正在尝试使用Portable Contacts API或完整的Contacts API获取对OAuth2经过身份验证的用户联系人的读取权限.谷歌最近开始允许使用OAuth2.
通过首先让用户对范围进行身份验证,我可以通过Contacts API访问用户联系人:"https://www.google.com/m8/feeds".然后我可以使用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
Run Code Online (Sandbox Code Playgroud)
这有效,我得到JSON数据.然而,令人难以置信的差不多,唯一的接触下令谷歌提供了(据我可以告诉)是" 上次更改 "(严重跆拳道?).我需要更像"顶级朋友"或"最受欢迎"的东西.
这恰好是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>
Run Code Online (Sandbox Code Playgroud)
这工作正常,我得到一个访问令牌传回.
接下来,我尝试向便携式联系人API发送ajax请求
$.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
Run Code Online (Sandbox Code Playgroud)
但是返回403错误
403 (The currently logged in user and/or the gadget requesting data, does not have access to people data.
Run Code Online (Sandbox Code Playgroud)
我有什么想法我做错了吗?
附录
I 在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
Run Code Online (Sandbox Code Playgroud)
但这让我得到了同样的403错误:
403 (The currently logged in user and/or the gadget requesting data, does not have access to people data
Run Code Online (Sandbox Code Playgroud)
问题是您显然无法在JSONP请求上设置请求标头.有关详细信息,请参阅此问题的答案.
据我所知,其他选择是:
谷歌不应该这么困难.
归档时间: |
|
查看次数: |
4167 次 |
最近记录: |