I can not get information with “DialogflowApp.getUser” method

ⅰ亾dé卋堺 提交于 2019-12-08 11:58:54

问题


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"
  }
}

回答1:


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});



回答2:


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

let accestoken = app.getUser().accessToken;


来源:https://stackoverflow.com/questions/47749476/i-can-not-get-information-with-dialogflowapp-getuser-method

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