2

我正在尝试通过 Android 上的 API 访问 goo.gl URLShortener。我正在使用 AccountManager 生成访问令牌:

String authTokenType = "oauth2:https://www.googleapis.com/auth/urlshortener";
String authToken = this.prefs.getString(Key.PREF_GOOGLE_ACCESS_TOKEN, "null");
if (!"null".equals(authToken)) {
  accountManager.invalidateAuthToken(authTokenType, authToken);
}
//(...)
accountManager.getAuthToken(account, authTokenType, false, new GetAuthTokenCallback(), null);

当我GetAuthTokenCallback使用生成的身份验证令牌运行 HTTP 请求以接收 URL 历史记录时,我得到状态401:unauthorized

HttpGet http_get = new HttpGet("https://www.googleapis.com/urlshortener/v1/url/history");
http_get.addHeader("Authorization", tokens[0]);

HttpResponse response;
response = GoogleOAuthOnPreferenceChangeListener.this.http_client.execute(http_get);
Log.d("tag", "Status: (" + response.getStatusLine().getStatusCode() + ") " + response.getStatusLine().getReasonPhrase());

JSON-Response 的内容是

{ 
  "error": {
    "errors": [
       {
         "domain": "global",
         "reason": "required",
         "message": "Login Required",
         "locationType": "header",
         "location": "Authorization"
       }
     ],
     "code": 401,
     "message": "Login Required"
   }
}

我有

4

1 回答 1

0

根据https://developers.google.com/identity/protocols/OAuth2ForDevices#callinganapi,您应该将标头设置为

Authorization: Bearer <access_token>

代替

Authorization: <access_token>
于 2015-06-10T02:55:02.840 回答