Dialogflow的Fullfilment逻辑无法获取Google帐户信息

时间:2017-12-11 13:43:41

标签: node.js firebase google-api google-oauth actions-on-google

我想使用Dialogflow的Fullfilment逻辑获取Google帐户信息,但会发生以下错误。

Error: connect ECONNREFUSED 127.0.0.1:443 at 
  Object.exports._errnoException (util.js:1020:11) at 
  exports._exceptionWithHostPort (util.js:1043:20) at 
  TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)

帐户关联已设置,在客户端,它已通过Google帐户认证。 我可以通过在浏览器中执行以下URL来获取用户信息。

https://www.googleapis.com/oauth2/v1/userinfo?access_token=foo_bar

但是,如果我在Fulfillment上访问上述URL,则会出现上述错误。 下面的定义是否有错误?

*使用Fullfilment,逻辑在index.js。

中描述

* index.js

'use strict';

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

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

  const app = new App({request:req, response:res});
  let accessToken = app.getUser().accessToken;
  console.log('accessToken is ' + accessToken);

  getUserInfo(accessToken).then((output) => {
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
  }).catch((error) => {
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
  });
};
function getUserInfo (accessToken) {
  return new Promise((resolve, reject) => {

    let https = require('https');
    let host = 'www.googleapis.com';
    let path = '/oauth2/v1/userinfo?access_token=' + accessToken;
    console.log('API Request: ' + host + path);

    https.get({hostName: host, path: path}, (res) => {
      let body = ''; // var to store the response chunks
      res.on('data', (d) => {
        body += d;
        console.log('data: ' + body);
      }); // store each response chunk
      res.on('end', () => {
        console.log('end');
        let output = `response`;
        console.log(output);
        resolve(output);
      });
      res.on('error', (error) => {
        reject(error);
      });
    });
  });
}

*的package.json

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

1 个答案:

答案 0 :(得分:0)

我发现你的编码错误了。

错字:

https.get({hostName: host, path: path}, (res) => {

正确的:

https.get({host: host, path: path}, (res) => {

请尝试以上方法。 感谢。