简体   繁体   English

云功能和jwt Google令牌请求

[英]cloud function and jwt Google token request

I know that I should use the cloud function service account to access to my bigquery but for my specific needs I want to have the choice of my service account. 我知道我应该使用云功能服务帐户来访问我的bigquery,但是出于我的特定需求,我想选择服务帐户。

So I decided to generate a token and use this token with the Google JWT client. 因此,我决定生成一个令牌,并将此令牌与Google JWT客户端一起使用。 The code (I retrieed in the Google example library) is working perfectly in local but when I try to deploy gcloud raises an error. 该代码(我在Google示例库中检索到的代码)在本地运行良好,但是当我尝试部署gcloud时出现错误。 I don't understand why and I don't know what way to explore to solve that. 我不明白为什么,也不知道探索解决该问题的方法。

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);
}

To deploy the function I use this syntax : 要部署功能,我使用以下语法:

gcloud functions deploy getGoogleToken --region=europe-west1 --memory=128MB --trigger-http --timeout=60

And I get this error : 我得到这个错误:

**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)

I suppose the library is not supported by Google cloud functions but it's a Google library? 我想该库不受Google云功能的支持,但它是Google图书馆吗?

Could someone help me please ? 有人可以帮我吗?

Have a great day 祝你有美好的一天

My bad sorry, but if someone is asking the same question just read that. 不好意思,但是如果有人问同样的问题,那就读一遍。

For the moment cloud function is working with nodejs Version 6 and this code is using await/async requires higher version of nodejs. 目前,云功能正在使用nodejs版本6,并且此代码正在使用await / async,需要更高版本的nodejs。 You can solve this problem by using beta (Be carefull beta means there is no garantee and no SLA) 您可以使用Beta解决此问题(请谨慎使用Beta,这意味着没有garantee和SLA)

the deployment syntax is this one : 部署语法是这样的:

gcloud beta functions deploy [functionName] --runtime nodejs8

Replace [functionName] by your function name. 用您的函数名称替换[functionName]。 Note the word "beta" in the second place of the syntax. 请注意语法的第二位单词“ beta”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM