[英]Google Cloud Platform Node.js Library
我正在創建一個谷歌功能。 但是,當我嘗試部署到Google Cloud Platform時,出現此錯誤
錯誤:(gcloud.beta.functions.deploy)OperationError:代碼= 3,消息=函數加載錯誤:無法加載文件index.js中的代碼。 您是否在package.json依賴項中列出了所有必需的模塊? 詳細的堆棧跟蹤:錯誤:找不到模塊“請求”
如何在Google Cloud Platform中上載/安裝“請求”庫?
'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);
});
});
}
-Alan-
閱讀有關依賴關系的Google Cloud文檔: https : //cloud.google.com/functions/docs/writing/dependencies
如果使用gcloud CLI,請在package.json文件中將“請求”模塊作為依賴項列出。
或者,在包含雲功能的文件夾中運行“ npm install --save request”,然后將預安裝的依賴項作為ZIP文件的一部分上載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.