【问题标题】:Google contacts API problems from JavaScript client with OAuth2 authenticationGoogle 通过 OAuth2 身份验证从 JavaScript 客户端联系 API 问题
【发布时间】:2011-11-16 01:35:40
【问题描述】:

现在已经为此苦苦挣扎了好几个小时,Docs 似乎很糟糕。基本上,我正在尝试使用Portable Contacts API 或完整的Contacts API 获得对经过OAuth2 身份验证的用户联系人的读取访问权限。 Google 已将 recently 启动为 allowing OAuth2

我可以通过联系人 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

这行得通,我得到 JSON 数据。然而,几乎令人难以置信的是,谷歌提供的唯一联系人订单(据我所知)是“lastmodified”(真的吗?)。我需要更像“顶级朋友”或“最受欢迎”的东西。

这恰好是 Google Portable Contacts API can do,(耶!)。当然,我似乎无法获得成功的工作请求。

首先,我通过单击此链接让用户使用便携式联系人 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>

这很好,我得到了一个传回的访问令牌。

接下来我尝试向便携式联系人 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

但这会返回 403 错误

403 (The currently logged in user and/or the gadget requesting data, does not have access to people data.

任何想法我做错了什么?

附录
我在 Google OAuth2 论坛中找到了this bug report,它建议我们在使用 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

【问题讨论】:

    标签: javascript oauth-2.0 google-contacts-api


    【解决方案1】:

    问题是您显然无法在 JSONP 请求上设置请求标头。有关更多信息,请参阅this question 上的答案。

    据我所知,替代方案是:

    1. 使用Google Contacts API JS library。那只使用谷歌自己建议的 AuthSub 是不好的。我宁愿不这样做。我与之交互的所有其他服务都使用 OAuth2。
    2. 使用我链接到的 SO 问题中提到的新的 2 级 Ajax 和 XDomainRequest 标准。然而,他们会带来自己的问题。这听起来像一团糟。它不适用于旧版浏览器,我必须进行大量功能检测等。我什至不知道 API 是否会支持这些功能。
    3. 在服务器上执行所有操作。这也不完全理想。不够完美的用户体验。

    Google 应该没有这么难的。

    【讨论】:

    • 这方面有什么更新吗?我一直在努力在客户端而不是在服务器中获取经过身份验证的用户的联系人。
    • 至少不是来自我。自从我回答了这个问题后,我不需要再试一次了。
    • 您可以在stackoverflow.com/questions/14104309/… 中查看我的答案,我相信它提供了完整的解决方案。令人惊讶的是,两年后,问题仍然存在。
    • 你能提取联系人照片吗?并更新联系电话?
    猜你喜欢
    • 1970-01-01
    • 2013-07-20
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 2016-01-22
    • 2018-06-17
    相关资源
    最近更新 更多