在服务器上GoogleAuthUtil.getToken()Google+的验证令牌时OAuth2ErrorException INVALID_TOKEN [英] OAuth2ErrorException invalid_token when verifying token on GoogleAuthUtil.getToken() Google+ on server

查看:2077
本文介绍了在服务器上GoogleAuthUtil.getToken()Google+的验证令牌时OAuth2ErrorException INVALID_TOKEN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google+ API的第一次。我有一个使用G +为身份验证的Web和Android客户端的应用程序。 Web客户端工作很好。我有一个在谷歌API控制台,我用一个JavaScript客户端来获取一个令牌设立针对Web应用程序客户端ID。

JavaScript客户端使用以下按钮(从谷歌演示应用程序基本上逐字):


 <按钮类=G-登入
       数据范围=htt​​ps://www.googleapis.com/auth/plus.login
       数据requestvisibleactions =htt​​p://schemas.google.com/AddActivity
       数据的clientId =my_web_application_client_id
       数据回调=onSignInCallback
       数据主题=黑暗
       数据的CookiePolicy =single_host_origin>


我验证与Python的服务器上:


  URL =('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s'%令牌)
 H =的httplib2.Http()
  结果= json.loads(h.request(URL,'GET')[1])


到目前为止好。令牌核实预期。

现在的Andr​​oid应用程序......我建立了一个用于调试的指纹所产生的同一个项目为Android应用程序客户端ID:
密钥工具-exportcert -alias androiddebugkey -keystore debug.keystore -list -v

该包的客户端id名称相匹配什么在我的Andr​​oid清单。

我使用GoogleAuthUtil产生在我的Andr​​oid应用程序令牌:

 字符串标记=
  GoogleAuthUtil.getToken(
    这个,
    selected@email.address
    的oauth2:服务器:CLIENT_ID:其中,CLIENT_ID计算值:+
      api_scope:HTTPS://www.googleapis.com/auth/plus.login);

如果我使用我的新创建的Andr​​oid应用客户端ID,我得到:
GoogleAuthException:com.google.android.gms.auth.GoogleAuthException:未知

我还没有想出一个尚未出来,但一些解决方案,暗示我应该只使用Web应用程序客户端ID和唯一的例外是如预期运行。

如果我用我的web应用程序的客户端ID,然后一切似乎只是正常工作并返回一个令牌,但是当我发送令牌到我的服务器来验证,该令牌被拒绝:

  {u'error_description':u'Invalid值,u'debug_info':U'code:INVALID_VALUE
http状态:400
参数:[INVALID_TOKEN]
原因:com.google.security.lso.protocol.oauth2.common.OAuth2ErrorException:\\
 在INVALID_TOKEN
com.google.security.lso.grant.token.TokenManager.getTokenInfo(TokenManager.java:603)在
com.google.security.lso.apiary.tokeninfo.TokenInfoAction.execute(TokenInfoAction.java:92)

我已经验证到达服务器令牌是一样的一个是在应用程序(无杂散空白等)获取的。

任何想法,我做错了什么?感谢您的任何建议!


解决方案

我最终什么事做的是以下内容:

当调用GoogleAuthUtil,而不是以下范围:


<$p$p><$c$c>oauth2:server:client_id:<client_id>:api_scope:https://www.googleapis.com/auth/plus.login


我用:


 观众:服务器:CLIENT_ID:其中,CLIENT_ID&GT;


其中CLIENT_ID是的网站的客户端ID。

这会产生令牌的要长得多。您可以解密/认证通过此令牌:


  https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=<token>


请注意,该查询参数关键是的 id_token 的,不是ACCESS_TOKEN。

服务器出现以下响应:


  {
 发行人:accounts.google.com
 ISSUED_TO:&所述; android_client_id&gt;中,
 观众:&LT; web_client_id&gt;中,
 USER_ID:&所述; some_long_integer&gt;中,
 expires_in:3354,
 issued_at:1406474776,
 电子邮件:&LT;传递给GaiaAuthUtil&GT的电子邮件地址;,
 verified_email:真
}


在你的服务器,你再:


  1. 验证了观众和ISSUED_TO字符串是你期待什么

  2. 验证用户名/电子邮件授权

再进行与请求处理。

这解决方案是从其他StackExchange的讨论和本教程信息的混合:
http://android-developers.blogspot.com/2013/01/verifying-back-end-calls-from-android.html

希望这会节约一些时间!

First time using the Google+ API. I have an application with a web and Android client that uses G+ for authentication. Web client is working nicely. I have a "Client ID for web application" set up in the Google API console, which I use to fetch a token with a javascript client.

The javascript client uses the following button (taken basically verbatim from a Google demo app):

<button class="g-signin"
       data-scope="https://www.googleapis.com/auth/plus.login"
       data-requestvisibleactions="http://schemas.google.com/AddActivity"
       data-clientId="my_web_application_client_id"
       data-callback="onSignInCallback"
       data-theme="dark"
       data-cookiepolicy="single_host_origin">

I verify on the Python server with:

url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s' % token)
 h = httplib2.Http()
  result = json.loads(h.request(url, 'GET')[1])

So far so good. Token verifies as expected.

Now for the android app... I set up a "Client ID for android application" for the same project with a debug fingerprint generated by: keytool -exportcert -alias androiddebugkey -keystore debug.keystore -list -v

The package for the client id name matches what's in my Android manifest.

I'm using GoogleAuthUtil to generate a token in my Android app:

String token =
  GoogleAuthUtil.getToken(
    this,
    "selected@email.address",
    "oauth2:server:client_id:<client_id>:" +
      "api_scope:https://www.googleapis.com/auth/plus.login");

If I use my my newly-created android app client id, I get: GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown

I haven't figured that one out yet, but some solutions hint that I should just be using the web application client id and that the Exception is working as intended.

If I use my web application client id, then everything appears to work just fine and a token is returned, but when I send the token to my server to verify, the token gets rejected:

{u'error_description': u'Invalid Value', u'debug_info': u'code: INVALID_VALUE
http status: 400
arguments: [invalid_token]
cause: com.google.security.lso.protocol.oauth2.common.OAuth2ErrorException:\
 invalid_token at
com.google.security.lso.grant.token.TokenManager.getTokenInfo(TokenManager.java:603) at
com.google.security.lso.apiary.tokeninfo.TokenInfoAction.execute(TokenInfoAction.java:92)

I've verified the token arriving at the server is the same one that is fetched in the app (no stray whitespace, etc).

Any ideas what I'm doing wrong? Thanks for any suggestions!

解决方案

What I ended up doing is the following:

When calling GoogleAuthUtil, instead of the following scope:

oauth2:server:client_id:<client_id>:api_scope:https://www.googleapis.com/auth/plus.login

I used:

audience:server:client_id:<client_id>

where client_id is the web client id.

This produces a much longer token. You can decrypt/authenticate this token via:

https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=<token>

Note that the query param key is id_token, not access_token.

The server responds with the following:

{
 "issuer": "accounts.google.com",
 "issued_to": "<android_client_id>",
 "audience": "<web_client_id>",
 "user_id": "<some_long_integer>",
 "expires_in": 3354,
 "issued_at": 1406474776,
 "email": "<email address passed to GaiaAuthUtil>",
 "verified_email": true
}

On your server, you then:

  1. Verify that the audience and issued_to strings are what you expect
  2. Verify the userid/email is authorized

then carry on with the request processing.

This solution is a mixture of information from other StackExchange discussions and this tutorial: http://android-developers.blogspot.com/2013/01/verifying-back-end-calls-from-android.html

Hope this will save someone some time!

这篇关于在服务器上GoogleAuthUtil.getToken()Google+的验证令牌时OAuth2ErrorException INVALID_TOKEN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆