Google通过OAuth2身份验证与JavaScript客户端联系API问题

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)

Dav*_*ite 6

问题是您显然无法在JSONP请求上设置请求标头.有关详细信息,请参阅此问题的答案.

据我所知,其他选择是:

  1. 使用Google Contacts API JS库.这只使用了谷歌自己暗示不好的AuthSub.我宁愿不这样做.我与之交互的每个其他服务都使用OAuth2.
  2. 使用我链接到的SO问题中提到的新的Level 2 Ajax和XDomainRequest标准.然而,他们会遇到自己的问题.整个听起来像是一团糟.它不适用于旧浏览器,我将不得不做一堆功能检测等.我甚至不知道API是否支持这些功能.
  3. 在服务器上完成所有操作.这也不完全理想.不那么完美的用户体验.

谷歌不应该这么困难.

  • 这有什么更新?我一直在努力在客户端获取经过身份验证的用户的联系人,而不是在服务器中进行. (3认同)