I can not get information with “DialogflowApp.getU

2019-06-14 06:52发布

https://developers.google.com/actions/reference/nodejs/ApiAiApp

I'd like to obtain an access token on Dialogflow by referring to the above official website, but the execution result of DialogflowApp.getUser() will be null.

Account linking is set up and on the client side it is certified with a Google Account. AssistantToAgentDebug.assistantToAgentJson.user on the DEBUG tab of DialogFlow Simulator contains a value.

Also, I can get an access token by referring req.body.originalRequest.data.user.accessToken variable on the request body.

I'd like to obtain user information with DialogflowApp.getUser(), Is there a mistake in the definition below?

*Using the Fullfilment, the logic is described in index.js.

*index.js

'use strict';

const App = require('actions-on-google').DialogflowApp;

exports.testMethod = (req, res) => {

  // result is null
  const app = new App({req, res});
  let user = app.getUser();
  console.log('User is ' + user);

  // Access token can be acquired
  let accessToken = req.body.originalRequest.data.user.accessToken;
  console.log('accessToken is ' + accessToken);

  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ 'speech': 'success!', 'displayText': 'success!' }));
};

*package.json

{
  "name": "testMethod",
  "engines": {
    "node": "~4.2"
  },
  "dependencies": {
    "actions-on-google": "^1.0.0",
    "firebase-admin": "^4.2.1",
    "firebase-functions": "^0.5.7"
  }
}

2条回答
Juvenile、少年°
2楼-- · 2019-06-14 07:18

You need to use getUser().accesToken like this:

let accestoken = app.getUser().accessToken;
查看更多
疯言疯语
3楼-- · 2019-06-14 07:37

The problem is that the call to the constructor expect the parameters to be named request and response. So you should write the line as

const app = new App({request:req, response:res});
查看更多
登录 后发表回答