谷歌任务身份验证与2腿oauth错误:401未经授权

时间:2013-07-23 11:50:14

标签: java authentication oauth google-api google-api-java-client

我正在使用此代码获取Google任务Api身份验证

import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.tasks.*;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.util.ArrayMap;

import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.services.tasks.model.TaskList;
import com.google.api.services.tasks.model.TaskLists;
import java.io.IOException;
import java.util.List;

public class GoogleConnection {
public static Tasks setup() throws Exception {
    com.google.api.services.tasks.Tasks tasks = null;
    HttpRequestFactory httpRequestFactory = null;
    HttpRequestInitializer httpRequestInitializer = null;
    OAuthHmacSigner signer = new OAuthHmacSigner();
    HttpTransport httpTransport = new NetHttpTransport();
    OAuthParameters oauthParameters = new OAuthParameters();
    final ArrayMap<String, Object> customKeys = new ArrayMap<String, Object>();

    customKeys.add("xoauth_requestor_id", "test.2@elmetni.mygbiz.com");
    signer.clientSharedSecret = "rdFB-_j_ysHBs51IpU_IWeWL";
    oauthParameters.version = "2.0";
    oauthParameters.consumerKey = "elmetni.mygbiz.com";
    oauthParameters.signer = signer;


     HttpRequestFactory requestFactory = httpTransport.createRequestFactory(oauthParameters);

     httpRequestInitializer = requestFactory.getInitializer();

    tasks = new  com.google.api.services.tasks.Tasks.Builder(httpTransport,  new JacksonFactory(), httpRequestInitializer)
            .setTasksRequestInitializer(new TasksRequestInitializer() {
              @Override
              public void initializeTasksRequest(TasksRequest<?> request) throws IOException  {
                @SuppressWarnings("rawtypes")
                TasksRequest tasksRequest =  (TasksRequest) request;
                tasksRequest.setUnknownKeys(customKeys);
                tasksRequest.setKey("AIzaSyCXedjBax4jxVQ146eSN1FU95iiUzEzeeM");
              }
            })
            .setApplicationName("first")
            .build();

    return tasks;
  }


public static List<com.google.api.services.tasks.model.Task> getTasksFromTaskList(String taskListId) throws Exception {
com.google.api.services.tasks.Tasks tasksService = GoogleConnection.setup();
com.google.api.services.tasks.model.Tasks result = tasksService .tasks().list(taskListId).execute();
return result.getItems();
}


 public static void main(String[] args) throws IOException, Exception {

getTasksFromTaskList("herewego");

 }
 }

我不断得到这个错误:

主题“main”中的异常com.google.api.client.googleapis.json.GoogleJsonResponseException:401未经授权 {   “代码”:401,   “错误”:[{     “域名”:“全球”,     “location”:“授权”,     “locationType”:“标题”,     “message”:“凭证无效”,     “reason”:“authError”   }],   “message”:“凭据无效” }

at:com.google.api.services.tasks.model.Tasks result = tasksService .tasks()。list(taskListId).execute();

有人可以告诉我它有什么问题吗?

1 个答案:

答案 0 :(得分:0)

线索在异常消息中:

  

消息“:”无效的凭据“,”原因“:”authError“

您用于进行身份验证的流程是旧的OAuth1.0 alpha规范,正如您在OAuthParameters的java文档中看到的那样。虽然我们可以尝试使您当前的代码工作,但最好使用标准的OAuth2.0规范。

如果您以前从未使用它,可能会有点混乱。开始使用OAuth2应用程序的最简单方法可能是使用https://developers.google.com/quickstart/处的快速入门工具。

在这里,您可以选择Tasks API和Java命令行平台,它将为已经连接了身份验证代码的您生成示例应用程序。它清楚地标记了您可以输入要进行的API调用的位置,并且您可以查看用于使用OAuth2.0进行身份验证的代码来学习基础知识。该工具不仅会为您提供代码,还会在API控制台上为您设置项目,为您提供使用API​​控制台授权自己的客户机密文件,并指导您如何执行示例

相关问题