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

时间:2019-06-11 13:15:28

标签: android json

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

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

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值并将其转换为布尔值?

2 个答案:

答案 0 :(得分:1)

.navigationBarItems(leading: PresentationButton( Image(systemName: "person.crop.circle") .imageScale(.large) .padding(), destination: ProfileHost() ) ) 返回的是Map类型的对象,因为您从函数中返回了一个对象。 JavaScript对象成为Java Maps。您只需像使用其他任何Map一样使用它即可。

result.getData()

答案 1 :(得分:0)

您在做什么就可以了,唯一缺少的是推广它,这可以解决问题

 return Promise.resolve({ message: “verification success”, verified: true })

与错误情况类似,但不使用Promise.resolve,而是使用Promise.reject