2017-08-28 54 views
0

我有一个简单的云端函数,它接收一个webhook,然后在BigQuery中执行一个流式插入。该代码是基于这个sample(除了我使用流插入)Cloud Functions中的间歇性身份验证错误

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

此功能以及大部分的时间,但我得到偶尔的身份验证错误,然后消失。 enter image description here

textPayload: "OTHER ERROR: {"code":401,"errors":[{"message":"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project .","domain":"global","reason":"unauthorized"}],"message":"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project ."}"

我不知道权威性如何能成为一个问题,因为云计算功能和BigQuery的都是在同一个项目。

+0

我强烈建议汇报[公开问题追踪]这个问题(https://cloud.google.com/support/docs/问题跟踪器)。 – Kenworth

+0

你多久会遇到这个错误?这可能是因为你的令牌过期了。 – Sonya

+0

@Sonya - 问题来了,随机时间消失,我不能指出任何具体的事情。由于云功能和BigQuery等其他服务都在单个GCP项目中运行,因此应该处理auth。 –

回答

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