带有AccountManager和OAuth2的Google URLShortener:401未经授权

时间:2013-01-25 17:10:51

标签: android oauth

我正在尝试通过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"
   }
}

我有

1 个答案:

答案 0 :(得分:0)

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

Authorization: Bearer <access_token>

而不是

Authorization: <access_token>