如何从 Parse Server 云代码 Parse.Cloud.httpRequest 中正确验证 Google API?

问题描述

我一直试图弄清楚如何使用我的 Google 服务帐户在 Parse.Cloud.httpRequest 中对我的 Google Api 进行身份验证,此处针对各种环境进行了描述:https://cloud.google.com/docs/authentication/production#auth-cloud-explicit-nodejs 另外,作为参考,{{ 3}} 描述了如何通过 REST 与 DialogFlow 交互。我可以使用 curl 从我的命令行成功获得来自 Dialogflow 的响应。解析云代码是我卡住的地方:我已将 googleapis npm 模块添加到 back4app cloud package.json。我不确定如何正确使用 Parse.Cloud.httpRequest 中的 google 服务帐户密钥(以 .json 文件的形式)作为标头中“授权”密钥的一部分。当我按原样(如下)运行时,我得到一个未定义/空的响应。有没有人成功做到这一点?

const googleServiceAccountKey = './<my .json google service account key file>';

Parse.Cloud.define("df-test",(request) => {
    Parse.Cloud.httpRequest({
        method: 'POST',url: 'https://dialogflow.googleapis.com/v2/projects/<my-project-id>/agent/sessions/1234567:detectIntent',headers: {
        'Content-Type': 'application/json','Authorization' : 'Bearer ' + googleServiceAccountKey,},body: {
            "query_input": {
                "text": {
                  "text": "Hello","language_code": "en-US"
                }
              }
        }
        }).then(function(httpResponse) {
        console.log(httpResponse.text);
        },function(httpResponse) {
        console.error('Request Failed with response code ' + httpResponse.status);
    });
 });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)