innFactory / react-native-dialogflow

A React-Native Bridge for the Google Dialogflow (API.AI) SDK
https://innfactory.de/softwareentwicklung/ui-ux/sprachsteuerung-mit-api-ai-in-einer-react-native-app/
MIT License
200 stars 64 forks source link

how to get accessToken for v2? #31

Closed eggybot closed 6 years ago

eggybot commented 6 years ago

Sorry for asking this but I tried to follow this steps. https://dialogflow.com/docs/reference/v2-auth-setup but I could not see/get the access token, I only got a json file plus a I check in google console and services but I could not locate any authentication token. gconsole Is there a way to generate a permanent token key?

I'm just asking this now due to I'm now switching my dialogflow to v2.

thanks,

glebez commented 6 years ago

Hey,

I was having the same issue and it seems, I figured it out.

First of all, you need to set up the gcloud SDK on your machine if you don't have it already. Follow the steps described here: https://cloud.google.com/sdk/docs/

After the gcloud is up and running, you need to run the following command:

gcloud auth activate-service-account --key-file=<service-account-key-file.json>

change the<service-account-key-file.json> to the path to the json file that you mention in your question.

After the above command succeeds, you can run

gcloud auth print-access-token

and use the output string as the first argument of Dialogflow_V2.setConfiguration method.

After I did it, my app communicates with the API correctly, but I am concerned if generating the Access Token this way and copying it to the config is a good idea. Yes, it works, but in the docs for print-access-token, they specify:

This command should be used sparingly and for debugging alone.

and this makes me think that this is not such a great solution.

If anyone could point out a better way, please, do.

glebez commented 6 years ago

Ok, I was a bit too fast with writing that reply ;)

Just figured out that those access tokens expire after an hour, so generating and copying them manually is definitely good for testing only.

eggybot commented 6 years ago

thanks @glebez

I'm still looking for a solution on how can the token access be permanent. Also version2 of Dialogflow was already released and I think we are using the v2beta of Dialogflow ('m not sure if v2 and v2beta has same structure of API)

will give more info if I found a solution.

thanks,

spoeck commented 6 years ago

@glebez thanks for your explanation above. If someone find a api call for gcloud to refresh the token we can build it into the react-native-dialogflow package. But I really found no better solution.

glebez commented 6 years ago

@eggybot, @spoeck Yeah, unfortunately this gcloud access token stuff is so unnecessarily complicated, compared to the way it was in v1. I guess it's for security sake, but still... I've plowed through tons of docs, but could not figure out a nice reliable way to generate the token to be used on the react native side.

And since I really need to build this thing on v2, I fell back to doing all the communications with dialogflow on the server with their node SDK for v2: https://github.com/dialogflow/dialogflow-nodejs-client-v2

Sadly, because I really like the ease of work with react-native-dialogflow.

spoeck commented 6 years ago

@glebez hmm.. do you know how the auth mechanism of the nodejs client works? If just have to make a rest call it wouldn't be a thing. I'm more into aws but they have for everything a rest interface.

glebez commented 6 years ago

@spoeck, the trick is, that before using the node client, you have to install and init gcloud SDK on your server and it somehow magically handles all the authentication for specific api calls for you behind the curtains... ¯\(ツ)

spoeck commented 6 years ago

@glebez oh fuck -> worst case >.<

But thanks for your research!

eggybot commented 6 years ago

still waiting for Google to release a much easier access like we have on V1. Right now the only way I can use v2 is via node client or php client

Upon looking at Dialogflow github section there are no V2 yet for Android or iOS implementation with V2.

either way I will stick V1 for integrating directly for react-native then will wait till Dialogflow team release a new version which we can assign/create the token within the phone.

thanks,

weikangchia commented 6 years ago

If you guys are still looking for a solution to generate access token to access DialogFlow V2 API. You may want to check out the following code (the library that is used in the code below is google-oauth-jwt):

const googleAuth = require('google-oauth-jwt');

function generateAccessToken() {
 return new Promise((resolve) => {
  googleAuth.authenticate(
   {
    email: <client_email>,
    key: <private_key>,
    scopes: 'https://www.googleapis.com/auth/cloud-platform',
   },
   (err, token) => {
    resolve(token);
   },
  );
 });
}

You may find your client_email and private_key from the JSON key file you have downloaded from your Google Cloud Platform project's service account page.

To find out which scope which scope you may need, you may checkout the DialogFlow V2 REST API documentation page.

Hope this help 😀

eggybot commented 6 years ago

@weikangchia gonna test it later, thanks for sharing it

spoeck commented 6 years ago

@weikangchia Thanks a lot !! I will test it today and keep you up to date

spoeck commented 6 years ago

finally it's solved yippi With the help of @weikangchia I built a react-native compatible auth mechanism for google tokens! In the version 3.1.0 you can set your credentials via:

 constructor(props) {
        super(props);

        Dialogflow_V2.setConfiguration(
            "your-dialogflow-project@asdf-76866.iam.gserviceaccount.com",
            '-----BEGIN PRIVATE KEY-----\nMIIEvgIBADAN...1oqO\n-----END PRIVATE KEY-----\n',
            Dialogflow_V2.LANG_GERMAN,
            'testv2-3b5ca'
        );
    }
tuanbku107 commented 6 years ago

i'm a newbie and I think should have a module to refresh access token...

Venryx commented 5 years ago

Thanks to the comments above, I put together a self-contained solution to in-browser access-token generation: https://stackoverflow.com/a/57857698/2441655

@tuanbku107 The answer above also handles refreshing the token once expired. (ie. after 60 minutes)

ajith-sankar commented 3 years ago

Dialogflow_V2.setConfiguration

please explain this method!