Hi so I'm implementing a Java Gmail API on a web server, and when try refresh the access token using a refresh token I have stored on the db I get the following error:
Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:326)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:346)
at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:577)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:494)
The code I use is:
InputStream in = GmailIntegration.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
Credential cred = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()).setTransport(
HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setTokenServerUrl(
new GenericUrl("https://accounts.google.com/o/oauth2/token"))
.setClientAuthentication(new BasicAuthentication(GoogleUtil.CLIENT_ID, GoogleUtil.CLIENT_SECRET))
.build()
.setRefreshToken(refreshToken);
cred.refreshToken();
return cred;
I did have this working using the GoogleAuthorizationCodeFlow but this code runs on a server so when this first runs it asks the user tho authenticate on a box they don't have access to.
And I have checked the scopes a few times and they look like they match up and we have a service account but I haven't used it since not all our users will be in our Gsuite account.
Any help would be very appreciated.