使用OAuth和AccountManager进行身份验证

时间:2012-08-22 14:50:37

标签: android oauth accountmanager

我在使用OAuth和YouTube进行身份验证时遇到问题。检索身份验证令牌 很好,但gdata.youtube.com声称auth密钥无效。我重新生成了 密钥使用invalidateAuthToken,因此密钥尚未过期。

我错过了什么?

选择帐户

Intent intent = accountManager.getAccountManager().newChooseAccountIntent(
    null, 
    null, 
    new String[]{"com.google"}, 
    true, 
    null,
    AUTH_TOKEN_TYPE, 
    new String[] {"text"}, 
    null);

startActivityForResult(intent, REQUEST_AUTHENTICATE);

检索身份验证令牌。

Bundle options = new Bundle();
options.putString("client_id", "67674xxxxxx.apps.googleusercontent.com");

accountManager.getAccountManager().getAuthToken(
    account, 
    AUTH_TOKEN_TYPE, 
    options, 
    (Activity)context, 
    accountManagerCallback, 
    null);

Http请求

URL feedUrl = new URL(build_query(action, arguments));

final HttpGet httpGet = new HttpGet(feedUrl.toURI());
HttpParams httpParameters = new BasicHttpParams();      
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

final DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

inputStream = timedCall(new Callable<InputStream>() {
    @Override
    public InputStream call() throws Exception {
            httpGet.addHeader("Authorization", "Bearer " + authAccount.getActiveAccountAuthToken());
            httpGet.addHeader("X-GData-Key", "key=");   

            HttpResponse response = httpClient.execute(httpGet);
            int responseCode = response.getStatusLine().getStatusCode();

            if(responseCode == 401) {
                response.getEntity().consumeContent();
                throw new AuthAccountException(responseCode);
            }
            return response.getEntity().getContent();
        }       
    },
    15, TimeUnit.SECONDS);

1 个答案:

答案 0 :(得分:0)

我使用了错误的范围。我没有收到访问令牌,而是收到了客户端登录令牌。 OAuth 2.0的正确范围是https://gdata.youtube.com