android - 从 Android 中的 Firebase Cloud Functions onCall 方法获取返回值的正确方法是什么?

标签 android json

我在 Cloud Functions 中创建了一个函数来验证购买签名。它必须返回一个 bool 值和一个字符串值。我在 Firebase Cloud Functions 文档中读到,我应该返回一个包含所有值的 JSON 结构。我是这样弄的。

编辑
这是我的整个云功能:

const BASE_64_ENCODED_PUBLIC_KEY = "MY_PUBLIC_KEY_HERE"
const KEY_FACTORY_ALGORITHM = "RSA-SHA1";

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
const crypto = require('crypto');

exports.verifySignature = functions.https.onCall((data, context) => {
    const purchaseJSON = data.signedData;
    const signature = data.signature;

    console.log("start verification");

    if (purchaseJSON === null || signature === null) {
        console.log("Purchase verification failed: missing data.");
        return {
            message: "missing data",
            verified: false
        }
    }

    const verifier = crypto.createVerify(KEY_FACTORY_ALGORITHM);

    verifier.update(purchaseJSON);
    if (verifier.verify(publicKey, signature, "base64")){
        console.log("signature verification success!");
        return {
            message: "verification success",
            verified: true
        }
    } else {
        console.log("signature verification failed!");
        return {
            message: "verification failed",
            verified: false
        };
    }
});

这是我在客户端上的代码:

private Task<String> verifyValidSignature(String signedData, String signature) {
    // Create the arguments to the callable function.
    Map<String, Object> data = new HashMap<>();
    data.put("signedData", signedData);
    data.put("signature", signature);

    return mFunctions.getHttpsCallable("verifySignature")
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    HttpsCallableResult result = task.getResult();
                    if (result != null) {
                        return result.getData().toString();
                    }

                    return null;
                }
            });
}

如何在 Android/Java 中获取 message 值并将其转换为字符串以及 verified 值并将其转换为 bool 值?

最佳答案

result.getData() 返回一个 Map 类型对象,因为您从函数返回了一个对象。 JavaScript 对象变成了 Java 映射。您只需像使用任何其他 map 一样使用它即可。

Map<String, Object> data = (Map<String, Object>) result.getData();
String message = (String) data.get("message");
boolean verified = (Boolean) data.get("verified");

关于android - 从 Android 中的 Firebase Cloud Functions onCall 方法获取返回值的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56544614/

相关文章:

c++ - 对 QJsonArray 进行排序

json - 通过Angular JS获取外部json文件

xcode - 将可变字典添加到可变字典以转换为 JSON

android - 在 LinearLayout 中放置 3 个按钮以占用相等的空间

android - 单击 DropdownButton 时应用程序崩溃 (Flutter)

android - Firebase 服务生命周期

android - 我怎样才能杀死我的 Android 平板电脑上除了我的应用程序之外的所有东西,并让它们全部死掉?

java - 通过用户输入保存音频文件android

python - 从字符串中获取有效的 python 列表(javascript 数组)

javascript - 可视化嵌套的 JSON 结构