【发布时间】: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