Hello,
I have a doubt regarding the access token returned by Nylas Auth when using OAuth Access Token flow.
I am trying to intgrate with Google and following are the integrate endpoints for OAuth integrations.
/oauth/login - starts the login flow
/oauth/exchange - exchange code for a access token & refresh token.
@Get("/oauth/login")
public ResponseEntity<?> login() {
List<String> scope = new ArrayList<>();
scope.add("https://www.googleapis.com/auth/userinfo.email");
scope.add("https://www.googleapis.com/auth/userinfo.profile");
scope.add("https://www.googleapis.com/auth/gmail.modify");
scope.add("https://www.googleapis.com/auth/gmail.compose");
scope.add("https://www.googleapis.com/auth/calendar");
UrlForAuthenticationConfig config = new UrlForAuthenticationConfig(NYLAS_CLIENT_ID,
"http://localhost:9090/oauth/exchange",
AccessType.OFFLINE,
AuthProvider.GOOGLE,
Prompt.DETECT,
scope,
true,
"sQ6vFQN",
null);
String url = nylas.auth().urlForOAuth2(config);
ResponseHeaders headers = ResponseHeaders.builder().status(302).add("location", url).build();
return ResponseEntity.of(headers);
}
@Get("/oauth/exchange")
public ResponseEntity<?> exchange(@Param("code") String code) {
assert code != null;
CodeExchangeRequest codeRequest = new CodeExchangeRequest(
"http://localhost:9090/oauth/exchange",
code,
NYLAS_CLIENT_ID,
NYLAS_API_KEY,
null);
String accessToken = null;
try{
CodeExchangeResponse codeResponse = nylas.auth().exchangeCodeForToken(codeRequest);
accessToken = codeResponse.getAccessToken();
}catch(Exception e){
throw new RuntimeException(e);
}
ResponseHeaders headers = ResponseHeaders.builder()
.status(302)
.add("location", "/")
.add("set-cookie", "access_token=%s".formatted(accessToken))
.build();
return ResponseEntity.of(headers);
}
I am exchanging the code for a token using the following code.
nylas.auth().exchangeCodeForToken()
The problem is that if I pass the returned token to Google’s endpoint to verify it, I get the invalid token error
curl "https://oauth2.googleapis.com/tokeninfo?access_token=<ACCESS_TOKEN>"
{
"error": "invalid_token",
"error_description": "Invalid Value"
}
My questions:
- Is the access token returned by Nylas the original access token returned by Google i.e. Nylas ask Google to provide token and returns the same value? Or Nylas keeps the Google returned token to itself & return a different token to the client?
- If its the second case, how can I find if the token is valid?