【问题标题】:Firebase Cloud Function undefined request query parameters from Android App来自Android App的Firebase Cloud Function未定义请求查询参数
【发布时间】: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


    【解决方案1】:

    无法使用 Firebase Functions SDK 调用 onRequest 类型的函数。 Firebase SDK 实现了您使用onCall 声明的callable function 的客户端。您在这里使用onRequest,这意味着您正在编写一个标准的HTTP 类型函数。对于该类型的函数,您应该使用标准 HTTP 客户端(而不是 Firebase SDK)。如果您确实想使用 Firebase SDK 来调用您的函数,则必须改为编写一个可调用函数。请注意可调用函数have their own spec,您将无法轻松地从邮递员调用它们。

    【讨论】:

      猜你喜欢
      • 2019-05-13
      • 2021-04-26
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多