簡體   English   中英

傳入的JSON對象不包含client_email字段

[英]The incoming JSON object does not contain a client_email field

我正在嘗試創建一個firebase雲功能。 所以我想在本地運行我的firebase雲功能。 但它不能如何設置身份驗證。

我安裝了firebase工具: https ://firebase.google.com/docs/functions/local-emulator我已經運行了命令firebase login ,所以現在我已經登錄了。 然后我通過本教程創建了我的json密鑰: https/home/$USER/.google/****.json現在,如果我輸入echo $GOOGLE_APPLICATION_CREDENTIALS ,結果為/home/$USER/.google/****.json包含的/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"

此外,我已經嘗試安裝完整的谷歌雲sdk和我運行: gcloud auth application-default login但沒有成功。

Npm包版本:

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

我想我已經提供了enought信息,但如果你願意,可以隨意問我。

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 serve和我觸發功能,我有這樣的錯誤:錯誤:傳入的JSON對象不包含client_email字段

我可以確保您的json文件包含client_email字段。

你能幫我用谷歌認證嗎?

謝謝你的幫助。

我遇到了類似的問題。 這可能是firebase-tools 7.0.2版中的一個錯誤。 我回滾到7.0.0版本,現在可以使用了。

所以臨時解決方案是:

npm i firebase-tools@7.0.0 -g  

簡而言之:

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

請參閱admin.credential.applicationDefault()的文檔

更新:請注意,這僅建議用於測試/試驗:

此策略在測試和試驗時非常有用,但可能很難分辨您的應用程序使用的憑據。 我們建議明確指定應用程序應使用哪些憑據,... 來源

多一點信息

當我嘗試在本地調用firebase函數時嘗試更新firestore數據庫中的某些文件時,我也有同樣的想法。 (沒有批次沒有測試)。

要在本地開始調用firebase函數,我使用:

firebase function:shell

您可能知道,這列出了項目的可用功能。

我調用了我的函數並得到以下錯誤callstack:

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()的文檔

您可能需要設置Firebase Admin SDK才能使用Firebase模擬器。 您可以通過在調用admin.initializeApp()方法時傳遞credential屬性來執行此admin.initializeApp()

const serviceAccount = require('../serviceAccount.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});

您可以在Firebase控制台中下載服務帳戶JSON文件:

  • 點擊“設置”圖標;
  • 轉到“用戶和權限”;
  • 點擊“N個服務帳戶也可以訪問此項目”的鏈接;
  • 單擊“生成新私鑰”按鈕。

創建Firebase項目后,您可以使用將服務帳戶文件與Google Application Default Credentials結合使用的授權策略初始化SDK。

要對服務帳戶進行身份驗證並授權其訪問Firebase服務,您必須生成JSON格式的私鑰文件。

要為您的服務帳戶生成私鑰文件:

  1. 在Firebase控制台中,打開“設置”>“服務帳戶”。

  2. 單擊“生成新私鑰”,然后單擊“生成密鑰”進行確認。

  3. 安全地存儲包含密鑰的JSON文件。

將環境變量GOOGLE_APPLICATION_CREDENTIALS設置為包含服務帳戶密鑰的JSON文件的文件路徑。 此變量僅適用於當前的shell會話,因此如果打開新會話,請再次設置該變量。

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

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

以下是我在經過幾個小時的努力后解決問題的方法:

簡短回答:

創建Firebase-adminsdk密鑰

怎么做:

  • 轉到Google-cloud-platform > Service accounts https://console.cloud.google.com/iam-admin/serviceaccounts/

  • 選擇您的項目

  • 選擇你的firebase-admin-sdk看起來像firebase-adminsdk-u4k3i@example..
  • 啟用編輯模式
  • Create key並選擇JSON
  • 您可以選擇下載.json 其中包含ProjectID, PrivateKey and ClientEmail
  • 在初始化應用時使用此類信息:
// Providing a service account object inline
admin.initializeApp({
    credential: admin.credential.cert({
        projectId: "<PROJECT_ID>",
        clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com",
        privateKey: "-----BEGIN PRIVATE KEY-----<KEY>-----END PRIVATE KEY-----\n"
    })
});

運行firebase emulators:start時出現此錯誤firebase emulators:start

根據此錯誤的調​​查: https//github.com/firebase/firebase-tools/issues/1451 ,這似乎是直接引用應用程序而不是通過管理模塊的問題。

即這會導致錯誤:

const app = admin.initializeApp();
const firestore = app.firestore();

但這不是:

admin.initializeApp();
const firestore = admin.firestore();

但是對於原始問題,您使用的是admin.firestore(),這樣就不會出現問題了。 似乎永遠不會調用admin.initializeApp() 也許這可能是你問題的原因?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM