【问题标题】:Android auth_token validation and usage server-sideAndroid auth_token 验证和使用服务器端
【发布时间】:2012-01-24 20:48:05
【问题描述】:

如何验证从

返回的 auth_token
token = bundle.getString(AccountManager.KEY_AUTHTOKEN);

?

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

我正在尝试像这样使用令牌:

url = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=%s' % access_token

但 Google 正在返回“未经授权的访问”。

如何访问

https://www.googleapis.com/oauth2/v1/userinfo

使用 Android AccountManager 提供的“auth_token”?

【问题讨论】:

  • 我不确定你是否可以将该令牌用于 Android 以外的其他服务,不过我可能是错的。
  • 我认为您缺少 'oauth2:' 范围前缀。请参阅下面的答案。

标签: android google-api oauth-2.0


【解决方案1】:

您应该能够使用它通过 Google API 获取用户信息。通常,在调用 RESTful API 时,会将 OAuth 2.0 不记名令牌插入到 Authorization HTTP 标头中。

请参阅此处提供的示例:http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager

【讨论】:

  • 获取用户没有问题,获取用户服务器端有问题,我需要证明 Android AccountManager 给我的 auth_token 是有效的。我不能只接受某人假装是电话发布到我的服务器的任何 auth_token,它需要以某种方式进行验证。
  • developer.android.com/training/id-auth/… 是一个例子,但我不知道“your_api_key”是什么,我需要使用联系人,而不是任务。
  • 我指向原始的示例表明:“一旦您的应用程序拥有最终用户的“身份验证令牌”,它应该将其传递给 GoogleHeaders.setGoogleLogin(String)。”我怀疑只是将访问令牌放入 HTTP 授权标头(例如:授权:承载您的访问令牌)。同样,这是您调用受 OAuth 保护的 API 的方式。标头中附加的令牌将向 Google 识别用户,并在您调用他们的服务时向您返回所需的用户信息。
  • 理论上,我应该可以在这里选择我的流​​服务器端:code.google.com/apis/accounts/docs/… ?
  • 是的,这行不通。我尝试使用 URL 字符串方法和 Authorization: Bearer 方法,都不适合我。无论哪种方式,我都会得到“未经授权”。
【解决方案2】:
【解决方案3】:

从 AccountManager 返回的令牌是 ClientLogin 令牌,而不是 OAuth 令牌。所以不能使用OAuth相关的API。但您仍然可以获取日历数据、联系人数据或 ClientLogin 支持的其他数据。

例如,如果您获取联系人的“cp”,则在服务器上,您可以使用

curl -H 'Authorization: GoogleLogin auth="Your_ClientLogin_token"' https://www.google.com//m8/feeds/contacts/default/full

使用 Zend Gdata,您可以这样做

$client = new Zend_Gdata_HttpClient;
$client->setClientLoginToken($token); // $token is your ClientLogin token
$gdata = new Zend_Gdata($client);

// perform query and get result feed
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full');
$feed = $gdata->getFeed($query);

但是,不建议在服务器端使用 ClientLogin 令牌,如下所述:

【讨论】:

    【解决方案4】:

    您可能只是缺少 authTokenType 前面的 oauth2: 前缀。

    此代码有效:

    // 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);
    

    }

    【讨论】:

    • 这就是答案,为什么我不必在API Console 注册我的应用程序。那是因为我没有访问任何 Google API 而是只获取 userinfo json?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    相关资源
    最近更新 更多