【问题标题】:Dialogflow easy way for authorizationDialogflow 简单的授权方式
【发布时间】:2018-11-05 20:11:57
【问题描述】:

是否存在将 Dialogflow 代理连接到 node.js 代码的简单方法?当我将此代码与从 Dialogflow 代理的设置页面获取的正确 projectID 一起使用时,出现以下错误:

错误:获取应用程序默认凭据时出现意外错误:无法加载默认凭据。浏览至https://developers.google.com/accounts/docs/application-default-credentials 了解更多信息。

const sessionClient = new dialogflow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);

我访问了该页面,但是对于我想要的内容我觉得很困惑(他们引用了其他 API 和很多设置),我该如何解决这个问题?

我想在不安装第三方 API 的情况下从文件中获取信息并全部加载。

【问题讨论】:

    标签: node.js dialogflow-es


    【解决方案1】:

    没有很好的文档记录,但最简单的身份验证方法是使用您的谷歌云平台控制台上提供的 JSON 文件。

    const sessionClient = new dialogflow.SessionsClient({
        keyFilename: '/path/to/google.json'
    });
    const sessionPath = sessionClient.sessionPath(projectId, sessionId);
    

    这也适用于所有其他客户端。 ContextsClientsEntityTypesClient 等等。

    【讨论】:

    • 谢谢,它应该可以工作,但我收到此错误:{ 错误:7 PERMISSION_DENIED: IAM 对 'projects/xxxx/agent' 的权限 'dialogflow.sessions.detectIntent' 被拒绝。我为测试创建了正确的访问密钥和文件,具有对话框流管理员和项目所有者的角色,但我仍然得到“访问被拒绝”,有什么想法吗?
    • 您缺少对 Google 云平台设置的一些权限。没有访问权限很难调试。
    • 您是否在IAM 选项卡中添加了对话流集成?
    • 感谢您的回答,现在我无法退房,我会尽快通知,感谢您的帮助和耐心
    • 好吧,你是对的,这有点分心,现在我解决了。谢谢
    【解决方案2】:

    我正在编写对我有用的代码。请按照Reference link 2 中提供的所有步骤进行操作,出于编码目的,您可以使用提供的 sn-p。

    我还添加了 Google Cloud Oauth 的示例 JSON

    参考文献:

    1. https://www.npmjs.com/package/dialogflow#samples
    2. https://medium.com/@tzahi/how-to-setup-dialogflow-v2-authentication-programmatically-with-node-js-b37fa4815d89

    //Downloaded JSON format
    
    {
      "type": "service_account",
      "project_id": "mybot",
      "private_key_id": "123456asd",
      "private_key": "YOURKEY",
      "client_email": "yourID@mybot.iam.gserviceaccount.com",
      "client_id": "098091234",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/yourID%40mybot.iam.gserviceaccount.com"
    }
    
    
    //------*********************---------------------------
    //
    const projectId = 'mybot';
    
    //https://dialogflow.com/docs/agents#settings
    // generate session id (currently hard coded)
    const sessionId = '981dbc33-7c54-5419-2cce-edf90efd2170';
    const query = 'hello';
    const languageCode = 'en-US';
    
    // Instantiate a DialogFlow client.
    const dialogflow = require('dialogflow');
    let privateKey = 'YourKey';
    
    // as per goolgle json
    let clientEmail = "yourID@mybot.iam.gserviceaccount.com";
    let config = {
      credentials: {
        private_key: privateKey,
        client_email: clientEmail
      }
    }
    const sessionClient = new dialogflow.SessionsClient(config);
    
    // Define session path
    const sessionPath = sessionClient.sessionPath(projectId, sessionId);
    
    // The text query request.
    const request = {
      session: sessionPath,
      queryInput: {
        text: {
          text: query,
          languageCode: languageCode,
        },
      },
    };
    
    // Send request and log result
    sessionClient
      .detectIntent(request)
      .then(responses => {
        console.log('Detected intent');
        const result = responses[0].queryResult;
        console.log(`  Query: ${result.queryText}`);
        console.log(`  Response: ${result.fulfillmentText}`);
        if (result.intent) {
          console.log(`  Intent: ${result.intent.displayName}`);
        } else {
          console.log(`  No intent matched.`);
        }
      })
      .catch(err => {
        console.error('ERROR:', err);
      });

    【讨论】:

    • 如何运行和测试这个??
    【解决方案3】:

    几个月前我有同样的问题,检查一下,这就是我解决它的方法。 Google Cloud 从您的 JSON 中提取这些行。

    const dialogflow = require('dialogflow');
    const LANGUAGE_CODE = 'en-US'
    const projectId = 'projectid';
    const sessionId = 'sessionId';
    const query = 'text to check';
    
    
    let privateKey = "private key JSON";
    let clientEmail = "email acount from JSON";
    let config = {
    credentials: {
        private_key: privateKey,
        client_email: clientEmail
    }
    };
    
    sessionClient = new dialogflow.SessionsClient(config);
    
    async function sendTextMessageToDialogFlow(textMessage, sessionId) {
    // Define session path
    const sessionPath = this.sessionClient.sessionPath(projectId, sessionId);
    // The text query request.
    const request = {
        session: sessionPath,
        queryInput: {
            text: {
                text: textMessage,
                languageCode: LANGUAGE_CODE
            }
        }
    }
    try {
        let responses = await this.sessionClient.detectIntent(request)
        console.log('DialogFlow.sendTextMessageToDialogFlow: Detected intent', responses);
        return responses
    } catch (err) {
        console.error('DialogFlow.sendTextMessageToDialogFlow ERROR:', err);
        throw err
    }
    };
    
    
    sendTextMessageToDialogFlow(query, sessionId)
    

    【讨论】:

      【解决方案4】:

      自最初的问题以来,Dialogflow 身份验证的文档已得到改进。您应该在这里找到所有答案:

      Authentication and access contro

      【讨论】:

        【解决方案5】:

        我遵循上述解决方案,几乎没有变化:

        // A unique identifier for the given session
        const sessionId = uuid.v4();
        
        // Create a new session
        
        const sessionClient = new dialogflow.SessionsClient({
            keyFilename: require("path").join('config/google-credential.json')
        });
        const sessionPath = sessionClient.sessionPath(process.env.DIALOGFLOW_PROJECTID, sessionId);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-05
          • 2011-02-24
          • 2019-06-23
          • 2019-04-17
          • 1970-01-01
          • 1970-01-01
          • 2010-10-24
          • 2016-04-28
          相关资源
          最近更新 更多