How to obtain a user ID and OAuth access token in Dialogflow
While I posted a comment on it here I still think there's an issue here as described in https://stackoverflow.com/questions/54871395/how-to-obtain-a-user-id-and-oauth-access-token-in-dialogflow.
Why should I be doing some coding acrobatics to get to accessToken, for example:
String accessToken = ((DialogflowRequest)request).getAogRequest().getUser().getAccessToken();
while the more straightforward methods advertised via the API don't work (always returning null):
String idToken = request.getUser().getIdToken();
log.info("request.getUser().getIdToken()={}",idToken);
Ditto for other fields, i.e. userId, sessionId, isSigninGranted, etc.
Or, am I missing something?
Just to confirm, the first snippet does work as expected?
Yes, this works:
String accessToken = ((DialogflowRequest)request).getAogRequest().getUser().getAccessToken();
but as it's been discovered via some deep debugging sessions and not really advertised anywhere, I'm cautious about using it as it may break in the future in case of impl change.
And this doesn't work: request.getUser().getIdToken()=null
Should it not return consistent results?
Check out the Google Sign In sample in Java but specifically here:
private boolean userIsSignedIn(ActionRequest request) {
String idToken = request.getUser().getIdToken();
LOGGER.info(String.format("Id token: %s", idToken));
if (idToken == null || idToken.isEmpty()) {
return false;
} else {
return true;
}
}
I'm closing this out but if you're still facing issues let us know.
@sarahdwyer I saw that, but it doesn't address the issue I'm experiencing
Did you set up Account Linking for your project? If so, what configuration did you choose (Google Sign-In, OAuth, etc.)?
We did via OAuth's Authorization code flow
@slkasisto if you added OAuth, then user info needs to come from your authentication/authorization provider. The access token you get back from request.getUser().getAccessToken() is the access token to your oauth provider
That is understood, however it was not populating.