【问题标题】:Calling cloud function admin.auth().createUser() from android does not work从 android 调用云函数 admin.auth().createUser() 不起作用
【发布时间】:2020-06-20 03:30:26
【问题描述】:

我正在尝试从不起作用的 Android 应用程序调用 Google 云功能(部署后的第一次调用在 90% 的情况下有效,但后续调用失败,firebase 日志控制台上也没有显示任何内容)。

public Task<String> myCloudFunction() {

    return FirebaseFunctions.getInstance()
            .getHttpsCallable("createUser")
            .call(data)
            .continueWith(task -> {
                String result = (String) task.getResult().getData();
                return result;
            });

}

函数仪表板中的端点 https://us-central1-xyz:555.cloudfunctions.net/createUser

我是这样称呼它的。

public void callCloudFunction() {

    createFirebaseUserAccount.myCloudFunction()
            .addOnCompleteListener(new OnCompleteListener<String>() {
                @Override
                public void onComplete(@NonNull Task<String> task) {
                    if (!task.isSuccessful()) {
                        Exception e = task.getException();
                        if (e instanceof FirebaseFunctionsException) {
                            FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                            FirebaseFunctionsException.Code code = ffe.getCode();
                            Object details = ffe.getDetails();
                        } else {
                            Timber.d(task.getResult());
                        }
                    }
                }
            });
}

这里是云函数: $GOOGLE_APPLICATION_CREDENTIALS 指向包含私钥的 service_key.json 文件。

admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: "https://XYZ.firebaseio.com"
});
exports.createUser = functions.https.onCall((data, context) => {
 const callerEmail = data.email;
 const callerPassword = data.password;
 const callerDisplayName = data.displayName;
 return admin.auth().createUser({ 
    email: callerEmail,
    emailVerified: false,
    password: callerPassword,
    displayName: callerDisplayName,
    disabled: false
   }).then(userRecord => {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log("Successfully created new user:", userRecord.uid);
        return userRecord.uid;

    }).catch(error => {
        console.log("Error creating new user ", error);
        return error;
    });
  });

感谢阅读! :)

【问题讨论】:

  • 请编辑问题以显示 Cloud Function 的代码,以及您使用 curl 执行的操作以调用它。
  • @DougStevenson 谢谢!我将云功能更改为后台触发器而不是 http 触发器并发布更多详细信息。
  • 仅供参考:可调用函数不被视为“背景”函数。它像 HTTP 函数一样直接调用。

标签: android firebase-authentication google-cloud-functions


【解决方案1】:

您没有从包含要发送给客户端的数据的函数返回一个承诺。实际上,您根本没有传递任何东西。你应该从你的异步工作中返回承诺链:

return admin.auth().createUser({ 
    email: callerEmail,
    emailVerified: false,
    password: callerPassword,
    displayName: callerDisplayName,
    disabled: false
   }).then(userRecord => {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log("Successfully created new user:", userRecord.uid);
        return userRecord.uid;

    }).catch(error => {
        console.log("Error creating new user ", error);
        return error;
    });

在整件事之前注意新的return。您绝对应该花一些时间来了解 JavaScript Promise 是如何工作的,以便有效地使用 Cloud Functions,如果不遵守它们的规则和约定,它们将无法正常工作。

【讨论】:

  • 问题解决了!我正在从测试类调用云函数,但它不起作用。但如果我从邮递员或活动中调用它,它会起作用。
猜你喜欢
  • 2014-08-23
  • 2019-01-18
  • 2017-08-09
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
相关资源
最近更新 更多