Android auth_token验证和使用服务器端

gdo*_*ald 3 android google-api oauth-2.0

如何验证从中返回的auth_token

token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
Run Code Online (Sandbox Code Playgroud)

我在Android上获得新用户后,我需要将它们插入我的数据库服务器端,但我需要在此之前以某种方式验证该令牌.

我试图像这样使用令牌:

url = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=%s' % access_token
Run Code Online (Sandbox Code Playgroud)

但谷歌正在返回"未经授权的访问".

我该如何访问

https://www.googleapis.com/oauth2/v1/userinfo
Run Code Online (Sandbox Code Playgroud)

使用Android AccountManager提供的'auth_token'?

Fre*_*uer 6

您可能只是oauth2:在authTokenType前面缺少前缀.

此代码有效:

// Note the `oauth2:` prefix
private static final String AUTH_TOKEN_TYPE_USERINFO_PROFILE =
    "oauth2:https://www.googleapis.com/auth/userinfo.profile";

// TODO: allow the use to choose which account to use
Account acct = accountManager.getAccountsByType("com.google")[0];

accountManager.getAuthToken(acct, AUTH_TOKEN_TYPE_USERINFO_PROFILE,
    null, this, new AccountManagerCallback<Bundle>() {
      @Override
      public void run(AccountManagerFuture<Bundle> future) {
        try {
          String accessToken = future.getResult().getString(
              AccountManager.KEY_AUTHTOKEN);
          Log.i(TAG, "Got OAuth2 access token: " + accessToken);
          /*
             Your code here. Use one of two options. In each case replace ... with
             the above OAuth2 access token:

             1) GET https://www.googleapis.com/oauth2/v1/userinfo?access_token=...

             2) GET https://www.googleapis.com/oauth2/v1/userinfo with this header:
                Authorization: Bearer ...
          */
        } catch (OperationCanceledException e) {
          // TODO handle this case
          Log.w(TAG, "The user has did not allow access");
        } catch (Exception e) {
          // TODO handle this exception
          Log.w(TAG, "Unexpected exception", e);
        }
      }
    }, null);
Run Code Online (Sandbox Code Playgroud)

}