简体   繁体   English

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

[英]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: 据我所知,替代方案是:

  1. Use the Google Contacts API JS library . 使用Google Contacts API JS库 That only uses AuthSub which google themselves suggest is bad. 那只使用谷歌自己暗示是不好的AuthSub。 I would rather not do this. 我宁愿不这样做。 Every other service I interact with uses OAuth2. 我与之交互的所有其他服务都使用OAuth2。
  2. Use the new Level 2 Ajax and XDomainRequest standards mentioned in the SO question I linked to. 使用我链接到的SO问题中提到的新的2级Ajax和XDomainRequest标准。 However they will come with their own problems. 但是,它们会带来自己的问题。 It sounds like a mess overall. 听起来整体上一团糟。 It won't work in older browsers and I'll have to do a bunch of feature detection etc. I don't even know if the API will support these features. 它在较旧的浏览器中将无法使用,我将不得不进行一系列功能检测等。我什至不知道API是否将支持这些功能。
  3. Do it all on the server. 在服务器上完成所有操作。 This isn't exactly ideal either. 这也不完全是理想的。 Less then perfect user experience. 不到完美的用户体验。

It shouldn't be this difficult Google. Google应该不难。

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

相关问题 Javascript客户端身份验证(使用SSL,没有OAuth2)? - Javascript Client Authentication (with SSL, without OAuth2)? Google oauth2 javascript客户端无法在IE 9中运行 - Google oauth2 javascript client not working in IE 9 适用于JavaScript API客户端的OAuth2流程 - Appropriate OAuth2 flow for JavaScript API client 使用Google JavaScript Client Lib的Google Contacts API - Google Contacts API with Google JavaScript Client Lib OAuth2身份验证js客户端 - OAuth2 authentication js client 在 Google 通讯录 API 版本 3.0 上使用 oauth2 检索刷新令牌 - Retrieving refresh token using oauth2 on Google Contacts API version 3.0 如何使用google-api-javascript-client或“Contacts API 3.0版”从gmail导入联系人? - How can i import contacts from gmail using google-api-javascript-client or “Contacts API version 3.0”? 如何使用Javascript从Google Api检索服务帐户OAuth2令牌? - How can I retrieve a service account OAuth2 token from Google Api with Javascript? 使用javascript API的Google Oauth2登录不适用于IE - Google Oauth2 sign in using javascript API is not working with IE OAuth Google Analytics Client API - OAuth Google Analytics Client API javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM