Spring Boot集成Google OAuth2获取用户邮箱时遭遇OAuth2Credentials不支持刷新令牌的问题求助
Spring Boot集成Google OAuth2获取用户邮箱时遭遇OAuth2Credentials不支持刷新令牌的问题求助
各位大佬好,我现在在做Spring Boot集成Google OAuth2的功能,想要获取用户的邮箱信息,但遇到了一个报错,卡了好一会儿了,特来求助!
报错信息
当执行到获取用户信息的代码时,抛出了如下错误:
{"errors":["OAuth2Credentials instance does not support refreshing the access token. An instance with a new access token should be used, or a derived type that supports refreshing."]}
问题代码
我的回调接口代码如下,报错具体出现在Userinfo userinfo = oauth2.userinfo().get().execute();这一行:
@GetMapping("/callback") public ResponseEntity<String> callback(@RequestParam(value = "code") String code) throws IOException { TokenResponse tokenResponse = authService.getTokenResponse(code, CALLBACK_URI); List<String> scopes = List.of("https://www.googleapis.com/auth/userinfo.email"); GoogleCredentials credentials = GoogleCredentials.create(new AccessToken(tokenResponse.getAccessToken(), null)); GoogleCredentials scopedCredential = credentials.createScoped(scopes); HttpRequestInitializer initializer = new HttpCredentialsAdapter(scopedCredential); Oauth2 oauth2 = new Oauth2.Builder(httpTransport, jacksonFactory, initializer) .setApplicationName("Test") // 替换为你的应用名称 .build(); Userinfo userinfo = oauth2.userinfo().get().execute(); // 此处报错 String email = userinfo.getEmail(); System.out.println(email); return ResponseEntity.status(HttpStatus.OK).build(); }
我的需求
我就是想通过Google OAuth2授权流程完成后,成功获取到用户的邮箱地址,现在卡在这个报错上了,有没有大佬能帮忙看看问题出在哪,该怎么解决呀?
备注:内容来源于stack exchange,提问作者zeppal
相关产品推荐
相关产品推荐

