【问题标题】:Google Tasks Authentication with 2 legged oauth error: 401 Unauthorized带有 2 条腿 oauth 错误的 Google Tasks 身份验证:401 Unauthorized
【发布时间】:2013-07-22 11:25:38
【问题描述】:

我正在使用此代码获取 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");

 }
 }

我一直在犯这个错误:

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

在:com.google.api.services.tasks.model.Tasks 结果 = tasksService .tasks().list(taskListId).execute();

有人能告诉我这有什么问题吗?

【问题讨论】:

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


    【解决方案1】:

    线索在异常信息中:

    message" : "Invalid Credentials", "reason" : "authError"

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

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

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

    【讨论】:

    • 我正在尝试 :D .. 并感谢您的建议 .. 再次感谢您的兄弟
    猜你喜欢
    • 2019-12-27
    • 2014-06-07
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2015-06-20
    • 2017-10-13
    • 2012-05-11
    • 2015-08-23
    相关资源
    最近更新 更多