我在使用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);