首页 > 解决方案 > 错误:传入的 JSON 对象在 JWT.fromJSON 中不包含 client_email 字段

问题描述

我正在尝试使用 Dialogflow 设置一个信使机器人,我在网络上找到了这个教程:https ://blog.pusher.com/facebook-chatbot-dialogflow/

我按照步骤设置 webhook,创建我的验证令牌,让我的 Facebook 页面订阅它,将我的服务器公开给 ngrok,并在 Dialogflow 中设置一些意图。

但是当我尝试发送消息时,我的 webhook 控制台会显示如下错误消息:

ERROR: Error: The incoming JSON object does not contain a client_email field
    at JWT.fromJSON (C:\Users\user\Desktop\MessageBot_DialogFlow\node_modules\google-auth-library\build\src\auth\jwtclient.js:193:19)
    at GoogleAuth._cacheClientFromJSON (C:\Users\user\Desktop\MessageBot_DialogFlow\node_modules\google-auth-library\build\src\auth\googleauth.js:313:16)
    at GoogleAuth.getClient (C:\Users\user\MessageBot_DialogFlow\node_modules\google-auth-library\build\src\auth\googleauth.js:494:22)
    at GrpcClient._getCredentials (C:\Users\user\MessageBot_DialogFlow\node_modules\google-gax\build\src\grpc.js:92:40)
    at GrpcClient.createStub (C:\Users\user\MessageBot_DialogFlow\node_modules\google-gax\build\src\grpc.js:213:34)
    at new SessionsClient (C:\Users\user\MessageBot_DialogFlow\node_modules\dialogflow\src\v2\sessions_client.js:159:34)
    at Object.<anonymous> (C:\Users\user\MessageBot_DialogFlow\src\process-message.js:17:23)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)

我不确定我的“process-message.js”是否有错误,这是我的代码:

const fetch = require('node-fetch');

// You can find your project ID in your Dialogflow agent settings
const projectId = 'messangerbot-fljyhy'; //https://dialogflow.com/docs/agents#settings
const sessionId = '123456';
const languageCode = 'en-US';

const dialogflow = require('dialogflow');

const config = {
    credentials: {
        private_key: process.env.DIALOGFLOW_PRIVATE_KEY,
        client_email: process.env.DIALOGFLOW_CLIENT_EMAIL
    }
};

const sessionClient = new dialogflow.SessionsClient(config);

const sessionPath = sessionClient.sessionPath(projectId, sessionId);

// Remember the Page Access Token you got from Facebook earlier?
// Don't forget to add it to your `variables.env` file.
const { FACEBOOK_ACCESS_TOKEN } = process.env;

const sendTextMessage = (userId, text) => {
    return fetch(
        `https://graph.facebook.com/v2.6/me/messages?access_token=${FACEBOOK_ACCESS_TOKEN}`,
        {
            headers: {
                'Content-Type': 'application/json',
            },
            method: 'POST',
            body: JSON.stringify({
                messaging_type: 'RESPONSE',
                recipient: {
                    id: userId,
                },
                message: {
                    text,
                },
            }),
        }
    );
}

module.exports = (event) => {
    const userId = event.sender.id;
    const message = event.message.text;

    const request = {
        session: sessionPath,
        queryInput: {
            text: {
                text: message,
                languageCode: languageCode,
            },
        },
    };

    sessionClient
        .detectIntent(request)
        .then(responses => {
            const result = responses[0].queryResult;
            return sendTextMessage(userId, result.fulfillmentText);
        })
        .catch(err => {
            console.error('ERROR:', err);
        });
}

我发现在 stackoverflow 上有一些类似的问题,但大多数问题都是关于 firebase。我不确定这些是相同的问题,所以我只是发布另一个问题。

这是我的 package.json 中的内容,有人可以告诉我问题出在哪里吗?谢谢。

{
  "name": "bot",
  "version": "1.0.0",
  "description": "n",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/nick880107-git/ProjectBot.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/nick880107-git/ProjectBot/issues"
  },
  "homepage": "https://github.com/nick880107-git/ProjectBot#readme",
  "dependencies": {
    "body-parser": "^1.19.0",
    "dialogflow": "^1.2.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "node-fetch": "^2.6.0"
  }
}

标签: node.jsdialogflow-esfacebook-messenger-bot

解决方案


推荐阅读