云功能和jwt Google令牌请求

时间:2018-08-02 08:50:55

标签: node.js google-cloud-functions google-api-nodejs-client

我知道我应该使用云功能服务帐户访问我的bigquery,但是出于我的特定需求,我想选择服务帐户。

因此,我决定生成一个令牌,并将此令牌与Google JWT客户端一起使用。该代码(我在Google示例库中检索到的代码)在本地运行良好,但是当我尝试部署gcloud时出现错误。我不明白为什么,也不知道探索解决该问题的方法。

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的库?

有人可以帮我吗?

祝你有美好的一天

1 个答案:

答案 0 :(得分:1)

对不起,但是如果有人问同样的问题,请阅读。

目前,云功能正在使用nodejs版本6,并且此代码正在使用await / async,需要更高版本的nodejs。 您可以通过使用Beta解决此问题(请谨慎使用Beta,这意味着没有garantee和SLA)

部署语法是这样的:

gcloud beta functions deploy [functionName] --runtime nodejs8

用您的函数名称替换[functionName]。 请注意语法的第二个位置“ beta”一词。

相关问题