4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Firebase Cloud Functionsをクライアントから呼び出すと謎の認証エラーと出くわした

Posted at

#Cloud Functionsをクライアントから呼ぶと謎のエラーが…

firebaseエミュレーターを使って、cloud functionsやらfirestoreやらを起動してテスト中に
クライアント(reactアプリ)から、下記のコードでcloud functionsの処理を呼び出すとエラーが…

###クライアント側

firebase.js
export const functions = firebase.functions("asia-northeast1");

if (window.location.hostname === "localhost") {
  functions.useFunctionsEmulator("http://localhost:5001");
}
app.js
  const handleEntry = () => {
    var onCall = functions.httpsCallable("entry");
    try {
      const result = await onCall({});
      console.log(result);
    } catch (e) {
      console.log(e);
    }
  };

###サーバー側

index.js
const admin = require("firebase-admin");
const functions = require("firebase-functions");

const serviceAccount = require("./hogehoge.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://hogehoge.firebaseio.com",
});

const db = admin.firestore();

exports.entry = functions
  .region("asia-northeast1")
  .https.onCall( (data, context) => {
    return {
      result: true,
      message: "OK",
    };
  });

##エラー内容

api.ts:54 POST https://fcmregistrations.googleapis.com/v1/projects/hogehoge/registrations 401
app.js:100 FirebaseError: Messaging: A problem occured while subscribing the user to FCM: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (messaging/token-subscribe-failed).

メッセージングで認証???
Cloud Messagingなんて使ってまへんけど…:confused:

###結局原因はわからん…:sob:

#解決方法
原因がわからんのに解決て…って感じですが、とりあえず回避する方法がわかったので残しておきます。
正式な回避方法がわかる方がいらっしゃったら、是非コメント頂けたらと…。

###悪いのはこいつっぽい
*とhogehogeでマスクしてます

firebase.js

const config = {
  apiKey: "***",
  authDomain: "hogehoge.firebaseapp.com",
  databaseURL: "https://hogehoge.firebaseio.com",
  projectId: "hogehoge",
  storageBucket: "hogehoge.appspot.com",
  messagingSenderId: "***", /* こいつがあると上記の認証エラーが出るのでこいつをとりあえずコメントアウト */
  appId: "***",
  measurementId: "***",
};

messagingSenderIdがconfigに指定されているとダメのようです。
今回はこれは使ってないのでコメントアウトして逃げます:sweat_smile:

Cloud Messaging使ってる場合はどうするんだろうか…

謎を残しつつ退散

4
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?