最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DialogFlow v2 Access Token cannot generate - Stack Overflow

programmeradmin0浏览0评论

With version 1 this is how I used to municate 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": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": ""
}

With version 1 this is how I used to municate 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./o/oauth2/auth",
  "token_uri": "https://accounts.google./o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis./oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis./robot/v1/metadata/x509/dialogflow-client%40xxxx"
}
Share Improve this question asked May 31, 2018 at 13:12 MizlulMizlul 2,29010 gold badges52 silver badges105 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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()).

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./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.

You might be interested in this:

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

发布评论

评论列表(0)

  1. 暂无评论