DialogFlow v2 Access Token cannot generate

╄→尐↘猪︶ㄣ 提交于 2019-12-30 10:46:50

问题


With version 1 this is how I used to communicate with DialogFlow Api!

fetch(configs.baseUrl + "query?v=20150910", {
    body: JSON.stringify({query: text, lang: "en", sessionId: "somerandomthing"}),
    headers: {
        'content-type': 'application/json',
        "Authorization": "Bearer " + configs.accessToken,
    },
    method: 'POST',
})
    .then(response => response.json())
    .then(data => {
        console.log(data.result.fulfillment.speech);
        return data.result.fulfillment.speech;
    })
    .catch(error => console.error(error))

I simply had to pass the access token into header and that was it!

I dont know how can I make this code work with DialogFlow v2, I am getting stuck on the access token, one my V2 Agents I can not longer see access token but instead I have a Project Id and Service Account.

I manage to create Service key from google console and activate thru gcloud but I just dont know where to get or how to generate this access token, or do I need an access token into v2, if not, how do I deal with this?

A working example would much appreciated.

Note I have downloaded this file which contains these kind of data and used this file in gcloud and it said that service activated smth but then what? is that all? what should I do next so I can make http call to V2 DialogFlow.

{
  "type": "service_account",
  "project_id": "xxxx",
  "private_key_id": "xxxx",
  "private_key": "-----BEGIN PRIVATE KEY-----xxxx",
  "client_email": "xxxx",
  "client_id": "xxxx",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dialogflow-client%40xxxx"
}

回答1:


You did pretty much the right thing, but you would probably want to use Dialogflows Node.js client SDK. These SDKs read the authentication JSON file automatically when you instantiate a client (see the example on Github, the file is read by ... = new dialogflow.SessionsClient()).




回答2:


Below is another example of creating your DialogFlow V2 access token using Node.js. 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. If you are unsure how/where to download it, you may checkout my blog post here.

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




回答3:


You might be interested in this:

https://cloud.google.com/docs/authentication/api-keys



来源:https://stackoverflow.com/questions/50625189/dialogflow-v2-access-token-cannot-generate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!