22

firebase クラウド関数を作成しようとしています。したがって、firebase クラウド関数をローカルで実行します。しかし、認証を設定する方法は機能しません。

firebase ツールをインストールしました: https://firebase.google.com/docs/functions/local-emulator コマンドfirebase loginを実行したので、ログに記録されました。次に、このチュートリアルで json キーを作成しました: https://cloud.google.com/docs/authentication/getting-started ここでecho $GOOGLE_APPLICATION_CREDENTIALS、結果を入力する/home/$USER/.google/****.jsonと、

"project_id","private_key_id","private_key","client_email", "client_id",  "auth_uri", "token_uri", "auth_provider_x509_cert_url", "client_x509_cert_url"

また、完全な Google Cloud SDK をインストールしようとしましたが、実行しましたgcloud auth application-default loginが、成功しませんでした。

Npm パッケージのバージョン:

"firebase-functions":"3.0.2"
"firebase-admin": "8.2.0"

十分な情報を提供したと思いますが、必要に応じてさらに質問してください。

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const express = require("express");
const app = express();
app.get("/", async (req, res) => {
admin.firestore().collection('something').get().then((collection) =>
    return res.send({"count": collection.docs.length, "status": 200});
});
exports.exports = functions.https.onRequest(app);

コードは重要ではありません。最も重要なことは、これらの手順をすべて実行しても、firebase をローカルでエミュレートしてfirebase serve関数をトリガーすると、次のエラーが発生することです: エラー: 着信 JSON オブジェクトには client_email が含まれていません分野

json ファイルに client_email フィールドが含まれていることを確認できます。

Google で認証する方法を教えてください。

ご協力いただきありがとうございます。

4

6 に答える 6

12

要するに:

admin.initializeApp({ credential: admin.credential.applicationDefault() });

admin.credential.applicationDefault()のドキュメントを参照してください

更新:これはテスト/実験にのみ推奨されることに注意してください:

この戦略は、テストや実験の際に役立ちますが、アプリケーションが使用している資格情報を特定するのが難しくなる可能性があります。アプリケーションが使用する資格情報を明示的に指定することをお勧めします...ソース

もう少し情報

バッチでfirestoreデータベースのいくつかのドキュメントを更新しようとするfirebase関数をローカルで呼び出そうとしたときにも同じことがありました。(バッチなしでテストしませんでした)。

ローカルで firebase 関数の呼び出しを開始するには、次を使用します。

firebase function:shell

ご存じのとおり、これはプロジェクトで使用可能な関数を一覧表示します。

関数を呼び出したところ、次のエラー コールスタックが表示されました。

Unhandled error Error: The incoming JSON object does not contain a client_email field
>      at JWT.fromJSON (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\jwtclient.js:165:19)
>      at GoogleAuth.fromJSON (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\googleauth.js:294:16)
>      at GoogleAuth.getClient (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\googleauth.js:476:52)
>      at GrpcClient._getCredentials (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-gax\build\src\grpc.js:107:40)
>      at GrpcClient.createStub (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-gax\build\src\grpc.js:223:34)
>      at new FirestoreClient (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\v1\firestore_client.js:128:39)
>      at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\index.js:315:26)
>      at ClientPool.acquire (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\pool.js:61:35)
>      at ClientPool.run (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\pool.js:114:29)
>      at Firestore.readStream (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\index.js:995:26)

RESPONSE RECEIVED FROM FUNCTION: 500, {
  "error": {
    "status": "INTERNAL",
    "message": "INTERNAL"
  }
}

コマンドラインを使用してローカルで関数を実行していました。 firebase functions:shell

私はこのコードを使用していました:

// Reference report in Firestore
const db = admin.firestore();

admin.initializeApp();

export const performMyCallableFirebaseFunction = (db, { from, to }) => {
    return db.collection("collectionName").where("prop", "==", from).limit(500).get().then(snapshot => {
        if (snapshot.empty) return new Promise(resolve => resolve(`No docs found with prop: ${from}`));

        const batch = db.batch();
        snapshot.forEach(doc => batch.update(doc.ref, { prop: to }));

        return batch.commit();
    });
};
exports.myCallableFirebaseFunction = functions.https.onCall(data => performMyCallableFirebaseFunction(db, data.from, data.to));

ラインを変えました

admin.initializeApp();

admin.initializeApp({ credential: admin.credential.applicationDefault() });

そして今、私は自分の関数をローカルで呼び出すことができました:

firebase functions:shell
firebase > myCallableFirebaseFunction({from: "foo", to: "bar"})

admin.credential.applicationDefault()のドキュメントを参照してください

于 2019-07-04T08:43:22.987 に答える
2

Firebase プロジェクトを作成したら、サービス アカウント ファイルと Google アプリケーションのデフォルト認証情報を組み合わせた認証戦略を使用して SDK を初期化できます。

サービス アカウントを認証し、Firebase サービスへのアクセスを承認するには、JSON 形式で秘密鍵ファイルを生成する必要があります。

サービス アカウントの秘密鍵ファイルを生成するには:

  1. Firebase コンソールで、[設定] > [サービス アカウント] を開きます。

  2. [Generate New Private Key] をクリックし、[Generate Key] をクリックして確認します。

  3. キーを含む JSON ファイルを安全に保管します。

環境変数 GOOGLE_APPLICATION_CREDENTIALS を、サービス アカウント キーを含む JSON ファイルのファイル パスに設定します。この変数は現在のシェル セッションにのみ適用されるため、新しいセッションを開く場合は、変数を再度設定してください。

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"

https://firebase.google.com/docs/admin/setup?authuser=3

于 2019-06-28T11:49:36.397 に答える