繁体   English   中英

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

[英]Google Calendar events not accessible with access token

我已经在 url https://console.developers.google.com 中创建了一个应用程序(选择“凭据”-->“创建凭据”-->“OAuth 客户端 ID”)并给出重定向 url 。 => 我得到了 client_id 和 client_secret,然后使用这些 client_id 和 client_secret 编写了一个小的 springboot 应用程序。

=> 运行项目,它被重定向到我配置的 url (localhost:8080/googleOauth/user)。 => 我在我的控制器中得到了带有 accessTokenValue 的 Principal 对象,通过使用这个 accessToken 我试图调用日历 api 来获取事件列表但得到错误

代码:

@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":{
      "code":403,
      "message":"Request had insufficient authentication scopes.",
      "errors":[
         {
            "message":"Insufficient Permission",
            "domain":"global",
            "reason":"insufficientPermissions"
         }
      ],
      "status":"PERMISSION_DENIED"
   }
}

请指导我哪里错了。

我的建议是您阅读Calendar API java quickstart 您走在正确的轨道上,因为您确实需要凭据。 但是您错过了授权过程中的一个重要部分,即您请求访问的范围,并且还必须为您为其创建凭据的项目启用这些部分。

这是关于授权对 Google 日历 API 的请求的指南。

暂无
暂无

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

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