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

Java Google任务身份验证与2腿oauth错误:401未经授权,java,authentication,oauth,google-api,google-api-java-client,Java,Authentication,Oauth,Google Api,Google Api Java Client,我使用这段代码来获取GoogleTasksAPI的身份验证 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.cli

我使用这段代码来获取GoogleTasksAPI的身份验证

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");

 }
 }
import com.google.api.client.http.*;
导入com.google.api.client.http.javanet.NetHttpTransport;
导入com.google.api.client.json.jackson.JacksonFactory;
导入com.google.api.services.tasks.*;
导入com.google.api.client.auth.oauth.oauthMacSigner;
导入com.google.api.client.util.ArrayMap;
导入com.google.api.client.auth.oauth.oauth参数;
导入com.google.api.services.tasks.model.TaskList;
导入com.google.api.services.tasks.model.TaskLists;
导入java.io.IOException;
导入java.util.List;
公共类谷歌连接{
公共静态任务安装程序()引发异常{
com.google.api.services.tasks.tasks tasks=null;
HttpRequestFactory HttpRequestFactory=null;
HttpRequestInitializer HttpRequestInitializer=null;
OAuthHmacSigner signer=新的OAuthHmacSigner();
HttpTransport HttpTransport=新的NetHttpTransport();
OAuthParameters OAuthParameters=新的OAuthParameters();
最终ArrayMap customKeys=新ArrayMap();
添加(“xoauth\u请求者\u id”,“测试”。2@elmetni.mygbiz.com");
signer.clientSharedSecret=“rdFB-_j_ysHBs51IpU_IWeWL”;
oauthParameters.version=“2.0”;
oauthParameters.consumerKey=“elmetni.mygbiz.com”;
oauthParameters.signer=签名者;
HttpRequestFactory requestFactory=httpTransport.createRequestFactory(oauthParameters);
httpRequestInitializer=requestFactory.getInitializer();
tasks=new com.google.api.services.tasks.tasks.Builder(httpTransport,new JacksonFactory(),httpRequestInitializer)
.setTasksRequestInitializer(新任务RequestInitializer(){
@凌驾
public void initializeTasksRequest(TasksRequest请求)引发IOException{
@抑制警告(“原始类型”)
TasksRequest TasksRequest=(TasksRequest)请求;
tasksRequest.setUnknownKeys(自定义密钥);
tasksRequest.setKey(“aizasycxedjbax4jxvq146esn1fu95iiiuzeem”);
}
})
.setApplicationName(“第一个”)
.build();
返回任务;
}
公共静态列表getTasksFromTaskList(字符串taskListId)引发异常{
com.google.api.services.tasks.tasks tasksService=GoogleConnection.setup();
com.google.api.services.tasks.model.tasks result=tasksService.tasks().list(taskListId.execute();
返回result.getItems();
}
公共静态void main(字符串[]args)引发IOException,异常{
获取任务列表(“herewego”);
}
}
我总是犯这样的错误:

线程“main”com.google.api.client.googleapis.json.GoogleJsonResponseException中的异常:401未经授权 { “代码”:401, “错误”:[{ “域”:“全局”, “位置”:“授权”, “位置类型”:“标题”, “消息”:“无效凭据”, “原因”:“authError” } ], “消息”:“无效凭据” }

位于:com.google.api.services.tasks.model.tasks result=tasksService.tasks().list(taskListId.execute()


谁能告诉我有什么问题吗

线索在异常消息中:

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

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

如果您以前从未使用过OAuth2应用程序,那么它的使用可能会有点混乱。使用OAuth2应用程序最简单的方法可能是在开始时使用Quickstart工具


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

我正在尝试:D..和thx…再次感谢兄弟