Google Tasks Authentication with 2 legged oauth er

2019-08-28 18:10发布

问题:

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

 }
 }

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 ?

回答1:

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 https://developers.google.com/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.