Firebase 云函数 - createCustomToken
Firebase Cloud Functions - createCustomToken
结合使用新的 Firebase Cloud Functions 和 admin sdk。
我想使用 admin.auth().createCustomToken() 函数。调用此函数会导致出现错误消息
Error: createCustomToken() requires a certificate with "private_key" set.
at FirebaseAuthError.Error (native)
at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23)
at FirebaseTokenGenerator.createCustomToken (/user_code/node_modules/firebase-admin/lib/auth/token-generator.js:62:19)
at Auth.createCustomToken (/user_code/node_modules/firebase-admin/lib/auth/auth.js:89:37)
at /user_code/index.js:29:26
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
如何配置云函数以使用 private_key?
admin.initializeApp(functions.config().firebase);
很遗憾,createCustomToken()
method requires a private key to mint custom tokens, which is not currently available with the default credential (which happens to be an Application Default Credential). As noted in Create custom tokens using the Firebase Admin SDKs,您需要提供证书凭据才能创建自定义令牌。
您可以按照 Add Firebase to your app 中的说明生成此凭据所需的证书。获得密钥 JSON 文件后,您需要将其放入 Cloud Functions for Firebase。
您可以将密钥 JSON 文件作为 service-account.json
存储在 /functions
文件夹中。然后,在您定义函数的文件中,使用 admin.credential.cert()
初始化 Admin SDK,如下所示:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
var serviceAccount = require("./service-account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: functions.config().firebase.databaseURL
});
有关如何执行此操作的完整示例,以及更详细的说明和代码示例,请查看 Instagram sign in sample。
请注意,我们希望将来从默认凭据添加对 createCustomToken()
的支持,但目前,您必须携带自己的凭据才能使用此特定方法。
如此处所述:https://firebase.google.com/docs/auth/admin/create-custom-tokens?authuser=0
To test the same code locally, download a service account JSON file and set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to it.
然后在你的代码中:
admin.initializeApp();
结合使用新的 Firebase Cloud Functions 和 admin sdk。
我想使用 admin.auth().createCustomToken() 函数。调用此函数会导致出现错误消息
Error: createCustomToken() requires a certificate with "private_key" set.
at FirebaseAuthError.Error (native)
at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23)
at FirebaseTokenGenerator.createCustomToken (/user_code/node_modules/firebase-admin/lib/auth/token-generator.js:62:19)
at Auth.createCustomToken (/user_code/node_modules/firebase-admin/lib/auth/auth.js:89:37)
at /user_code/index.js:29:26
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
如何配置云函数以使用 private_key?
admin.initializeApp(functions.config().firebase);
很遗憾,createCustomToken()
method requires a private key to mint custom tokens, which is not currently available with the default credential (which happens to be an Application Default Credential). As noted in Create custom tokens using the Firebase Admin SDKs,您需要提供证书凭据才能创建自定义令牌。
您可以按照 Add Firebase to your app 中的说明生成此凭据所需的证书。获得密钥 JSON 文件后,您需要将其放入 Cloud Functions for Firebase。
您可以将密钥 JSON 文件作为 service-account.json
存储在 /functions
文件夹中。然后,在您定义函数的文件中,使用 admin.credential.cert()
初始化 Admin SDK,如下所示:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
var serviceAccount = require("./service-account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: functions.config().firebase.databaseURL
});
有关如何执行此操作的完整示例,以及更详细的说明和代码示例,请查看 Instagram sign in sample。
请注意,我们希望将来从默认凭据添加对 createCustomToken()
的支持,但目前,您必须携带自己的凭据才能使用此特定方法。
如此处所述:https://firebase.google.com/docs/auth/admin/create-custom-tokens?authuser=0
To test the same code locally, download a service account JSON file and set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to it.
然后在你的代码中:
admin.initializeApp();