Java 无法使用访问令牌访问Google日历事件
我已经在url中创建了一个应用程序(选择“凭据”-->“创建凭据”-->“OAuth客户端ID”),并给出了重定向url。 =>我获得了客户机id和客户机机密,然后使用这些客户机id和客户机机密编写了一个小型springboot应用程序 =>运行该项目并将其重定向到我配置的url(localhost:8080/googleOauth/user)。 =>我在控制器中获得了带有accessTokenValue的主体对象,通过使用此accessToken,我试图调用calendar api来获取事件列表,但得到错误 代码:Java 无法使用访问令牌访问Google日历事件,java,spring-boot,google-calendar-api,Java,Spring Boot,Google Calendar Api,我已经在url中创建了一个应用程序(选择“凭据”-->“创建凭据”-->“OAuth客户端ID”),并给出了重定向url。 =>我获得了客户机id和客户机机密,然后使用这些客户机id和客户机机密编写了一个小型springboot应用程序 =>运行该项目并将其重定向到我配置的url(localhost:8080/googleOauth/user)。 =>我在控制器中获得了带有accessTokenValue的主体对象,通过使用此accessToken,我试图调用calendar 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"
}
}
错误:
@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"
}
}
请告诉我哪里错了。我的建议是你仔细检查一下。你走在正确的轨道上,因为你确实需要凭证。但是,您缺少授权过程中的一个重要部分,即您请求访问的作用域,并且对于您为其创建凭据的项目,也必须启用这些作用域
以下是有关的指南。您授权的范围如何?您授权应用程序使用哪些作用域?我没有授权任何具有任何作用域的应用程序,如果需要,如何使用rest api授权?任何例子都会对我有所帮助。