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

编程入门 行业动态 更新时间:2024-10-26 19:37:59
本文介绍了谷歌任务验证与2腿oauth错误:401未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

import com.google.api.client.http 。*; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson.JacksonFactory; 导入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签名者=新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); signer.clientSharedSecret =rdFB-_j_ysHBs51IpU_IWeWL; oauthParameters.version =2.0; oauthParameters.consumerKey =elmetni.mygbiz; oauthParameters.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)请求; tasksRequest.setUnknownKeys (customKeys); tasksRequest.setKey(AIzaSyCXedjBax4jxVQ146eSN1FU95iiUzEzeeM); } }) .setApplicationName(first) .build(); 返回任务; } public static List< com.google.api.services.tasks.model.Task> getTasksFromTaskList(String 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(); $ b public static void main(String [] args)throws IOException异常{ getTasksFromTaskList(herewego); $ b $ / code $ / pre $ b $ p and im keep getting this erreur:

线程main中的异常com.google.api.client.googleapis.json.GoogleJsonResponseException:401未授权 {code: 401,错误:[{domain:global,location:Authorization,locationType:header,message:Invalid Credentials,reason:authError}],message:无效凭证}

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

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

解决方案

线索出现在异常消息中:

message:无效凭证,reason:authError

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

如果以前从未使用它,可能会有点令人困惑。可能是最简单的方法开始OAuth2应用程序将使用快速入门工具,位于 developers.google/quickstart/。

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

i am using this code to get an Authentication for Google tasks 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"); signer.clientSharedSecret = "rdFB-_j_ysHBs51IpU_IWeWL"; oauthParameters.version = "2.0"; oauthParameters.consumerKey = "elmetni.mygbiz"; 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"); } }

and i m keep getting this erreur :

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized { "code" : 401, "errors" : [ { "domain" : "global", "location" : "Authorization", "locationType" : "header", "message" : "Invalid Credentials", "reason" : "authError" } ], "message" : "Invalid Credentials" }

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

Can some one pls tell me what s wrong about it ?

解决方案

The clue is in the exception message:

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

The flow you're using to authenticate is the old OAuth1.0 alpha spec, as you can see in the java doc for OAuthParameters. While we could try to make your current code work, it would be better to use the standard OAuth2.0 specification.

It can be a little confusing to use, if you've never worked with it before. Probably the easiest way to get started with an OAuth2-application is to use the Quickstart tool at developers.google/quickstart/.

Here you can choose the Tasks API and the Java command-line platform, and it will generate a sample application for you that already has the authentication code wired up. It clearly marks where you can input the API call you want to make, and you can look through the code they used to authenticate using OAuth2.0 to learn the basics. The tool will not only provide you with the code, but also set up a project for you on the APIs console, provide you with the client secrets file you need to authorize yourself with the APIs console, and guide you on how to execute the sample.

更多推荐

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

本文发布于:2023-11-09 22:42:39,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未经授权   错误   oauth

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!