简体   繁体   English

无法使用访问令牌访问 Google 日历活动

[英]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 .我已经在 url https://console.developers.google.com 中创建了一个应用程序(选择“凭据”-->“创建凭据”-->“OAuth 客户端 ID”)并给出重定向 url 。 => I got the client_id and client_secret then written a small springboot application by using these client_id and client_secret. => 我得到了 client_id 和 client_secret,然后使用这些 client_id 和 client_secret 编写了一个小的 springboot 应用程序。

=> Ran the Project and it is redirected to my configured url (localhost:8080/googleOauth/user). => 运行项目,它被重定向到我配置的 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 => 我在我的控制器中得到了带有 accessTokenValue 的 Principal 对象,通过使用这个 accessToken 我试图调用日历 api 来获取事件列表但得到错误

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 .我的建议是您阅读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 .这是关于授权对 Google 日历 API 的请求的指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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