Android 通过HttpClient连接到Google Tasks API时发生身份验证错误

Android 通过HttpClient连接到Google Tasks API时发生身份验证错误,android,oauth-2.0,Android,Oauth 2.0,我试图通过使用HttpClient连接到Google任务,但总是得到“未经授权的HttpResponseException” 下面是我在AccountManagerCallback的“run”方法中的代码 @Override public void run(AccountManagerFuture<Bundle> result) { // Get the result of the operation from the AccountManagerFutur

我试图通过使用HttpClient连接到Google任务,但总是得到“未经授权的HttpResponseException”

下面是我在AccountManagerCallback的“run”方法中的代码

 @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();
        }


    }
@覆盖
公共作废运行(AccountManagerFuture结果){
//从AccountManagerFuture获取操作的结果。
束;
试一试{
bundle=result.getResult();
token=bundle.getString(AccountManager.KEY\u AUTHTOKEN);
Log.i(“########,token);//token确实有值
字符串url=”https://www.googleapis.com/tasks/v1/users/@me/lists?key=“+您的api\u key;
HttpGet getRequest=新的HttpGet(url);
addHeader(“客户机id”,即您的客户机id);
addHeader(“客户机密码”,即您的客户机密码);
getRequest.addHeader(“授权”、“OAuth”+令牌);
HttpClient HttpClient=新的DefaultHttpClient();
String ResponseBy=httpclient.execute(getRequest,new BasicResponseHandler());//此处引发异常
Log.i(“####”,responseBody);//无法在此处获取响应
}捕捉(操作取消异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(认证异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
我使用的是谷歌API 10级仿真器,它确实可以访问互联网,并且我在其中添加了一个谷歌帐户


感谢您为我的代码提供帮助或建议。谢谢。

getRequest.addHeader(“授权”、“OAuth”+令牌);--我的直觉是问题就在这里。您确定要在令牌前面加上“OAuth”吗?谢谢您的回复。是的,我很确定“OAuth”应该在那里。我发现问题出在无效令牌上。我只需在emulator中删除该帐户并再次添加,这就解决了我的问题。ThanksgetRequest.addHeader(“授权”、“OAuth”+令牌);--我的直觉是问题就在这里。您确定要在令牌前面加上“OAuth”吗?谢谢您的回复。是的,我很确定“OAuth”应该在那里。我发现问题出在无效令牌上。我只需在emulator中删除该帐户并再次添加,这就解决了我的问题。谢谢