【发布时间】:2021-03-06 15:04:49
【问题描述】:
我有这个 Firebase 云函数:
exports.verifyToken = functions.https.onRequest(async (req, res) => {
const token = req.query.token;
console.log("token: " + token);
return firebaseAdmin
.auth()
.createCustomToken(token, {provider: 'TEST'})
.then((firebaseToken) => {
console.log("Returning firebase token to user: " + firebaseToken);
return res.json({firebase_token: firebaseToken});
});
});
这是我的安卓代码:
return Single.create<String> { emitter ->
val token = authToken.accessToken
val data = HashMap<String, String>()
data.put("token", token)
FirebaseFunctions.getInstance()
.getHttpsCallable("verifyToken")
.call(data)
.continueWith { task ->
return@continueWith task.result?.data as String
}
.addOnSuccessListener { firebaseToken ->
emitter.onSuccess(firebaseToken)
}
.addOnFailureListener {
emitter.onError(it)
}
}
当我尝试通过 firebase 模拟器和 Postman 运行云功能时,它运行良好。该函数能够获取token 的值。但每当我通过 Android 执行此操作时,我都会从 Firebase 函数日志中获取:
4:29:40.837 AM
verifyToken
Function execution started
4:29:41.411 AM
verifyToken
token: undefined //<--- this here says that the token is undefined.
4:29:41.554 AM
verifyToken
Unhandled rejection
4:29:41.560 AM
verifyToken
Error: `uid` argument must be a non-empty string uid. at FirebaseAuthError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:43:28) at FirebaseAuthError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:89:28) at new FirebaseAuthError (/workspace/node_modules/firebase-admin/lib/utils/error.js:148:16) at FirebaseTokenGenerator.createCustomToken (/workspace/node_modules/firebase-admin/lib/auth/token-generator.js:233:19) at Auth.BaseAuth.createCustomToken (/workspace/node_modules/firebase-admin/lib/auth/auth.js:96:36) at /workspace/index.js:25:6 at cloudFunction (/workspace/node_modules/firebase-functions/lib/providers/https.js:51:16) at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:100:17 at processTicksAndRejections (internal/process/task_queues.js:79:11)
4:29:41.562 AM
verifyToken
Function execution took 725 ms, finished with status: 'crash'
如果您已经注意到,代码几乎与 Firebase 在其入门页面中的代码相似。但它并不能完全发挥我的作用。
- 我已经检查过我的
google-services.json并且它已经更新了。 - 我目前被设置为项目的所有者,所以
firebase deploy没有问题 - 我还更新了
service-account.json,并将其包含在 firebase 函数中。
我可能遗漏了我的代码或配置中的某些内容。非常感谢任何输入!
【问题讨论】:
标签: android firebase firebase-authentication google-cloud-functions