Node.js 云函数和jwt Google令牌请求
我知道我应该使用云功能服务帐户来访问我的bigquery,但出于我的特殊需要,我想选择我的服务帐户 所以我决定生成一个令牌,并在GoogleJWT客户端上使用这个令牌。代码(我在Google示例库中重试)在本地运行得很好,但当我尝试部署gcloud时会出现错误。我不明白为什么,也不知道该用什么方法来解决这个问题Node.js 云函数和jwt Google令牌请求,node.js,google-cloud-functions,google-api-nodejs-client,Node.js,Google Cloud Functions,Google Api Nodejs Client,我知道我应该使用云功能服务帐户来访问我的bigquery,但出于我的特殊需要,我想选择我的服务帐户 所以我决定生成一个令牌,并在GoogleJWT客户端上使用这个令牌。代码(我在Google示例库中重试)在本地运行得很好,但当我尝试部署gcloud时会出现错误。我不明白为什么,也不知道该用什么方法来解决这个问题 const {JWT} = require('google-auth-library'); exports.getGoogleToken = (req,res) => {
const {JWT} = require('google-auth-library');
exports.getGoogleToken = (req,res) => {
const client = new JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/cloud-platform','https://www.googleapis.com/auth/bigquery','https://www.googleapis.com/auth/bigquery.insertdata']
);
await client.authorize();
const url = `https://www.googleapis.com/bigquery/v2/projects`;
const response = await client.request({url});
authorization=response.config.headers.Authorization;
res.status(200).send(authorization);
}
要部署函数,我使用以下语法:
gcloud functions deploy getGoogleToken --region=europe-west1 --memory=128MB --trigger-http --timeout=60
我得到了这个错误:
**ERROR**: (gcloud.functions.deploy) OperationError: code=3, message=Function
load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:32
await client.authorize();
^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at getUserFunction (/var/tmp/worker/worker.js:388:24)
我想Google云函数不支持这个库,但它是Google库
有人能帮我吗
祝你有一个美好的一天我很抱歉,但是如果有人问同样的问题,请阅读 目前云函数正在使用nodejs版本6,并且此代码正在使用wait/async,因此需要更高版本的nodejs。 您可以通过使用beta解决这个问题(小心beta意味着没有担保和SLA) 部署语法如下所示:
gcloud beta functions deploy [functionName] --runtime nodejs8
将[functionName]替换为您的函数名。
注意语法第二位的单词“beta”