繁体   English   中英

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

[英]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请求上设置请求标头。 有关更多信息,请参见此问题的答案。

据我所知,替代方案是:

  1. 使用Google Contacts API JS库 那只使用谷歌自己暗示是不好的AuthSub。 我宁愿不这样做。 我与之交互的所有其他服务都使用OAuth2。
  2. 使用我链接到的SO问题中提到的新的2级Ajax和XDomainRequest标准。 但是,它们会带来自己的问题。 听起来整体上一团糟。 它在较旧的浏览器中将无法使用,我将不得不进行一系列功能检测等。我什至不知道API是否将支持这些功能。
  3. 在服务器上完成所有操作。 这也不完全是理想的。 不到完美的用户体验。

Google应该不难。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM