【问题标题】:Intermittent authentication errors in Cloud FunctionsCloud Functions 中的间歇性身份验证错误
【发布时间】:2018-02-05 23:55:46
【问题描述】:

我有一个简单的云函数,它接收一个 webhook,然后将流式插入 BigQuery。代码基于这个sample(除了我使用的是流式插入)

exports.webHook = function webHook (req, res) {
  return Promise.resolve()
    .then(() => {
      if (req.method !== 'POST') {
        const error = new Error('Only POST requests are accepted');
        error.code = 405;
        throw error;
      }

      const events = req.body || {};
      if (events) {
        const opts = { ignoreUnknownValues: true };
        bigquery
          .dataset('config.DATASET')
          .table('config.TABLE')
          .insert(events, opts)
          .then((data) => {
            console.log(`Success: ${JSON.stringify(data[0])}`);
          })
          .catch((error) => {
            if (error.name === 'PartialFailureError') {
              console.error(`PARTIAL ERROR: ${JSON.stringify(error)}`);
              } else {
              console.error(`OTHER ERROR: ${JSON.stringify(error)}`);
            }

          });
      };
    })
    .then(() => res.status(200).end())
    .catch((err) => {
      console.error(err);
      res.status(err.code || 500).send(err);
      return Promise.reject(err);
    });
};

此功能在大多数情况下运行良好,但我确实偶尔会遇到身份验证错误,然后就消失了。

textPayload: "其他错误: {"code":401,"errors":[{"message":"Request 具有无效的身份验证凭据。预期的 OAuth 2 访问令牌, 登录 cookie 或其他有效的身份验证凭据。看 https://developers.google.com/identity/sign-in/web/devconsole-project.","domain":"global","re​​ason":"unauthorized"}],"message":"请求 具有无效的身份验证凭据。预期的 OAuth 2 访问令牌, 登录 cookie 或其他有效的身份验证凭据。看 https://developers.google.com/identity/sign-in/web/devconsole-project."}"

我不确定身份验证如何成为问题,因为 Cloud Function 和 BigQuery 都在同一个项目中。

【问题讨论】:

  • 我强烈建议在Public Issue Tracker 上报告该问题。
  • 您多久收到一次此错误?可能是您的令牌即将过期。
  • @Sonya - 问题来来去去都是随机的,我无法指出任何具体的问题。由于云功能和 BigQuery 等其他服务都在单个 GCP 项目中运行,因此应该处理身份验证。

标签: javascript google-bigquery google-cloud-functions


【解决方案1】:

Cloud Functions 团队的人员认为这可能是由于访问令牌的生存时间 (TTL) 存在问题,并建议了一个对我有用的解决方法。不要在代码顶部初始化 BigQuery(正如他们所有的示例都有),而是将初始化代码放在进行调用的函数中。

这样做:

exports.webHook = function webHook (req, res) {
  const bigquery = require('@google-cloud/bigquery')();
  return Promise.resolve()
    .then(() => {
      if (req.method !== 'POST') {
        const error = new Error('Only POST requests are accepted');
        error.code = 405;
        throw error;
      }
      .
      .

代替:

const bigquery = require('@google-cloud/bigquery')();
.
.
exports.webHook = function webHook (req, res) {
      return Promise.resolve()
        .then(() => {
          if (req.method !== 'POST') {
            const error = new Error('Only POST requests are accepted');
            error.code = 405;
            throw error;
          }
          .
          .

【讨论】:

    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 2018-07-09
    • 2018-07-23
    • 2016-12-31
    • 2020-10-25
    • 2022-10-20
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多