node.js - Dialogflow的Fullfilment逻辑无法获取Google帐户信息

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

最佳答案

我发现你的编码错误。

拼写错误:

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

正确:

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

请尝试以上操作。 谢谢。

关于node.js - Dialogflow的Fullfilment逻辑无法获取Google帐户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47754305/

相关文章:

javascript - .findOne 不是函数

node.js - 尝试将 bcrypt 安装到 Node 项目 + Node 设置问题

javascript - 我如何绕过 firebase 在响应时给出的数组中的随机 id?

google-api - Google Drive API 失败,因为库的 google.drive 函数不存在

css - 为什么 google 字体 oswald 在 Firefox 和 Chrome 中不显示,但在其他网络浏览器中显示?

javascript - Koa 和 Stripe,等待渲染页面

html - 如何在使用更改的 html 文件时保留套接字 io ID?

arrays - 使用 Firebase 查询数组值

java - Android/Firebase - 检查您是否订阅了主题

ios - 适用于 iOS 的 Google OAuth 2.0/JWT 身份验证示例