when calling refreshToken(), I get the message:
com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized POST https://oauth2.googleapis.com/token
Here is my Credential creation code. I get accessToken and refreshToken using Spring Security. The application runs successfully until the expiration of the accessToken
public void initCredential() {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (ObjectUtils.isEmpty(properties)) {
properties = createAdsProperties(managerProvider);
}
authorizedClient = authorizedClientService.loadAuthorizedClient(
ID_CLIENT_PROVIDER,
authentication.getName());
credential = new GoogleCredential.Builder()
.setClientSecrets(properties.getProperty(CLIENT_ID),
properties.getProperty(CLIENT_SECRET))
.setJsonFactory(JSON_FACTORY)
.setTransport(GoogleNetHttpTransport.newTrustedTransport())
.build()
.setAccessToken(authorizedClient.getAccessToken().getTokenValue())
.setRefreshToken(authorizedClient.getRefreshToken().getTokenValue());
// TokenResponse response =
// new GoogleRefreshTokenRequest(GoogleNetHttpTransport.newTrustedTransport(),
// JSON_FACTORY,
// authorizedClient.getRefreshToken().getTokenValue(),
// properties.getProperty(CLIENT_ID),
// properties.getProperty(CLIENT_SECRET)).execute(); //TokenResponseException 401
credential.refreshToken(); //TokenResponseException 401
} catch (IOException | GeneralSecurityException e) {
String errorMessage = e.getMessage();
log.error(errorMessage);
show(errorMessage);
throw new CustomException(errorMessage, e);
}
}
the commented-out code also doesn't work
I have also aligned the areas in the code and in my account https://console.cloud .google.com/
spring.security.oauth2.client.registration.google.client-id=xxxx
spring.security.oauth2.client.registration.google.client-secret=xxxx
spring.security.oauth2.client.registration.google.redirect-uri=http://localhost:8080/login/oauth2/code/google
spring.security.oauth2.client.registration.google.scope=https://www.googleapis.com/auth/userinfo.email,\
openid,\
https://www.googleapis.com/auth/userinfo.profile
spring.security.oauth2.client.provider.google.token-uri=https://oauth2.googleapis.com/token
spring.security.oauth2.client.provider.google.authorization-uri=https://accounts.google.com/o/oauth2/auth?prompt=consent&access_type=offline
spring.security.oauth2.client.provider.google.user-info-uri=https://www.googleapis.com/oauth2/v3/userinfo
please help me!
Updated
example of DriveService initialization code
driveService = new Drive.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
GoogleConnectionService.JSON_FACTORY, googleConnectionService.getCredential())
.setApplicationName(GoogleConnectionService.APPLICATION_NAME)
.build();
driveService.channels().stop(channel).execute();
if you delete the credential.refreshToken() line of code, the code will work successfully... within 1 hour, the user can call the Google api after that, the message above will appear Q: how do I update the access token?