actions-on-google-java icon indicating copy to clipboard operation
actions-on-google-java copied to clipboard

How to obtain a user ID and OAuth access token in Dialogflow

Open ghost opened this issue 6 years ago • 8 comments

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?

ghost avatar Feb 27 '19 22:02 ghost

Just to confirm, the first snippet does work as expected?

Fleker avatar Feb 28 '19 14:02 Fleker

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?

ghost avatar Feb 28 '19 17:02 ghost

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.

findzaatar avatar Mar 29 '19 19:03 findzaatar

@sarahdwyer I saw that, but it doesn't address the issue I'm experiencing

ghost avatar Mar 29 '19 19:03 ghost

Did you set up Account Linking for your project? If so, what configuration did you choose (Google Sign-In, OAuth, etc.)?

taycaldwell avatar Apr 07 '19 03:04 taycaldwell

We did via OAuth's Authorization code flow

ghost avatar Apr 07 '19 03:04 ghost

@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

morphine9 avatar May 28 '19 16:05 morphine9

That is understood, however it was not populating.

ghost avatar May 29 '19 11:05 ghost