2017-08-28 4 views
0

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

に基づいています。この機能は十分にほとんどの時間を動作しますが、私はそれから消える時折認証エラーを得ますか。 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のようなクラウド機能などのサービスはすべて1つのGCPプロジェクト内で実行されるため、authが処理されるはずです。 –

答えて

1

クラウドファンクションチームのメンバーは、これがアクセストークンの有効期間(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; 
      } 
      . 
      . 
関連する問題