我在使用 OAuth 和 YouTube 进行身份验证时遇到问题。授权令牌的检索顺利,但 gdata.youtube.com 声称授权密钥无效。我已经使用 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);