【Firebase】firebase projects:list で「HTTP Error: 401, Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.」が発生する

はじめに

こんにちは、SHOJIです。

Firebaseで発生したエラー対応の備忘録です。

firebase projects:list 実行でエラーが発生

久しぶりにFirebaseのソースを直そうと思い、作業ディレクトリで firebase projects:list コマンドを叩いてみたら「Update available 8.2.0 → 8.5.0 ││ Run npm i -g firebase-tools to update」とのメッセージが表示されました。


指示通り npm i -g firebase-tools to update を実行してツールを更新し、再度コマンドを実行するも今度は「Error: Failed to list Firebase projects. See firebase-debug.log for more info.」とのエラーが発生。


これまた指示に従ってデバッグログを確認すると、次のようなエラーメッセージが……。

[debug] [2021-01-11T14:57:02.753Z] HTTP Error: 401, 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. [debug] [2021-01-11T14:57:02.956Z] FirebaseError: HTTP Error: 401, 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. at module.exports (/opt/node-v12.16.3-linux-x64/lib/node_modules/firebase-tools/lib/responseToError.js:38:12) at Request._callback (/opt/node-v12.16.3-linux-x64/lib/node_modules/firebase-tools/lib/api.js:41:35) at Request.self.callback (/opt/node-v12.16.3-linux-x64/lib/node_modules/firebase-tools/node_modules/request/request.js:185:22) at Request.emit (events.js:310:20) at Request.EventEmitter.emit (domain.js:482:12) at Request.<anonymous> (/opt/node-v12.16.3-linux-x64/lib/node_modules/firebase-tools/node_modules/request/request.js:1154:10) at Request.emit (events.js:310:20) at Request.EventEmitter.emit (domain.js:482:12) at IncomingMessage.<anonymous> (/opt/node-v12.16.3-linux-x64/lib/node_modules/firebase-tools/node_modules/request/request.js:1076:12) at Object.onceWrapper (events.js:416:28) at IncomingMessage.emit (events.js:322:22) at IncomingMessage.EventEmitter.emit (domain.js:482:12) at endReadableNT (_stream_readable.js:1187:12) at processTicksAndRejections (internal/process/task_queues.js:84:21)


とりあえず、指示に従おうと「See https://developers.google.com/identity/sign-in/web/devconsole-project.」に記載のURLへアクセスして試行錯誤するも上手く行かず。

そもそもログインしてないのでは?と firebase login してみるものの、「Already logged」と言われる状態。


打つ手がないので一旦 firebase CLI を消そうと考え、一応その前にログインし直してみようと、firebase logout して firebase login したところ事象が解消されました。


釈然としなくて firebase の token 絡みのトラブルについてググったら、一時間程度でトークンの有効期限が切れるという記事を見つけました。が、これまでは何時間、あるいは何日経っても問題なかったのに、どうして今回はエラーになったのかは分からずじまいでした(Firebase側で何か変更があったとか?)。


この件はあまりしっかり調べる気になれなかったので、中途半端ですが対処法だけ残して終えたいと思います。

以上です。