我正在尝试使用 HttpClient 连接到 Google Tasks,但总是得到“未经授权的 HttpResponseException”。
这是我在 AccountManagerCallback 的“运行”方法中的代码
@Override
public void run(AccountManagerFuture<Bundle> result) {
// Get the result of the operation from the AccountManagerFuture.
Bundle bundle;
try {
bundle = result.getResult();
token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
Log.i("#######", token); // token does have value
String url = "https://www.googleapis.com/tasks/v1/users/@me/lists?key=" + your_api_key;
HttpGet getRequest = new HttpGet(url);
getRequest.addHeader("client_id", your_client_id);
getRequest.addHeader("client_secret", your_client_secret);
getRequest.addHeader("Authorization", "OAuth " + token);
HttpClient httpclient = new DefaultHttpClient();
String responseBody = httpclient.execute(getRequest, new BasicResponseHandler()); // exception raised here
Log.i("###", responseBody); // cannot get the response here
} catch (OperationCanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticatorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我正在使用可以访问互联网的 Google APIs Level 10 模拟器,并且我确实在其中添加了一个 google 帐户。
感谢任何帮助或建议使我的代码正常工作。谢谢。