gpt4 book ai didi

node.js - 谷歌云平台 Node.js 库

转载 作者:搜寻专家 更新时间:2023-11-01 00:36:30 24 4
gpt4 key购买 nike

我正在创建一个谷歌功能。但是,当我尝试部署到 Google Cloud Platform 时,出现此错误

错误:(gcloud.beta.functions.deploy) OperationError:代码=3,消息=函数加载错误:无法加载文件 index.js 中的代码。您是否在 package.json 依赖项中列出了所有必需的模块?详细堆栈跟踪:错误:找不到模块“请求”

如何在谷歌云平台上传/安装“请求”库?

代码片段

'use strict';
const https = require('https');
const host = 'https://www.example.com';
const clientId = 'qpopMIGtVdeIdVk3oEtr2LGbn8vTeTWz';
const clientSecret = 'eUnsWQ8y3AuiFHJu';
const grant_type = 'client_credentials';
const resource = 'b.microsoft.com/4fa4b4a7-d34f-49af-8781-c8b39f0cf770';
const request = require("request");


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

// Call the Apigee API
callGetOAuthToken().then((output) => {
// Return the results from the APigee to DialogFlow
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
}).catch((error) => {
// If there is an error let the user know
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
});
};
function callGetOAuthToken () {
return new Promise((resolve, reject) => {

let path = '/customers/v1/accesstoken';

var authHeader = Buffer.from(clientId + ':' + clientSecret).toString('base64');
var post_options = {
url: host + path,
method: 'POST',
headers:
{
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + authHeader,
'grant_type':grant_type
}
};

// Make the HTTP request to get the weather
request(post_options, function(err, res, body) {
let output = JSON.parse(body);
console.log(output);
resolve(output);
});
});
}

-艾伦-

最佳答案

通读有关依赖项的 Google Cloud 文档: https://cloud.google.com/functions/docs/writing/dependencies

如果使用 gcloud CLI,请将“请求”模块列为 package.json 文件中的依赖项。

或者,在包含您的云功能的文件夹中运行“npm install --save request”,并将预安装的依赖项作为 ZIP 文件的一部分上传。

关于node.js - 谷歌云平台 Node.js 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49870892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com