简体   繁体   中英

Google Calendar events not accessible with access token

i have created an application into the url https://console.developers.google.com (select "Credentials" --> "Create credentials" --> "OAuth client ID") and given the redirect url . => I got the client_id and client_secret then written a small springboot application by using these client_id and client_secret.

=> Ran the Project and it is redirected to my configured url (localhost:8080/googleOauth/user). => i got the Principal object with accessTokenValue in my controller, by using this accessToken i tried to call calendar api to get the list of events but getting error

Code:

@RequestMapping(value = "/user")
public Principal user(Principal principal) {
    System.out.println("*******************************principal *\n " + principal);
    System.out.println(principal.getName());
    
    if (principal != null) {
        
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
        OAuth2AuthenticationDetails oauthDetls = ((OAuth2AuthenticationDetails) oAuth2Authentication.getDetails());
        System.out.println("Bearer Token => "+oauthDetls.getTokenType()+" "+oauthDetls.getTokenValue());
        

    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credentials = new GoogleCredential.Builder()
           .setTransport(httpTransport)
           .setJsonFactory(jsonFactory)
           .setClientSecrets("1048632525615-ib80u833vjpou4dglknsa007kpo4gbqs.apps.googleusercontent.com", "ZH0ln3VtYNISWBiSZrOmd21u")
           .build();
    credentials.setAccessToken(oauthDetls.getTokenValue());
               

    Calendar service = new Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(), new JacksonFactory(), 
            credentials)
            .build();
    // Retrieve an event
    Events event = service.events().list("primary").execute();
    System.out.println("Events Summary => "+event.getSummary());

    
    }
    
    return principal;
}

Error:

{
   "error":{
      "code":403,
      "message":"Request had insufficient authentication scopes.",
      "errors":[
         {
            "message":"Insufficient Permission",
            "domain":"global",
            "reason":"insufficientPermissions"
         }
      ],
      "status":"PERMISSION_DENIED"
   }
}

please guide me where i am wrong .

My recommendation is that you go through the Calendar API java quickstart . You are on the right track, as you do need credentials. But you are missing an important part in the authorization process which are the scopes you are requesting to access, and these have to be enabled also for the project for which you created the credentials.

Here is a guide on Authorizing requests to the Google Calendar API .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM