代码之家  ›  专栏  ›  技术社区  ›  Hatchi Roku

Android获得唯一的FireBase令牌并使用云功能进行身份验证

  •  1
  • Hatchi Roku  · 技术社区  · 7 年前

    我正在构建一个多玩家的Android游戏,它使用FireBase云功能。 有一些教程解释了如何只允许我的应用程序的用户使用我的云功能(链接如下),但我不想允许所有用户使用我的所有功能,我想基于ID授予访问权限。如何从Android为每个用户生成唯一的令牌( Using Java not Kotlin )以及如何从令牌中获取ID node.js (Javascript not TypeScript) 是吗?

    教程链接: https://firebase.google.com/docs/cloud-messaging/auth-server

    1 回复  |  直到 7 年前
        1
  •  2
  •   Yosi Pramajaya    7 年前

    启动firebase函数1.0+,有两种HTTP函数可用于Android应用程序。

    1. 直接调用函数。通过 functions.https.onCall
    2. 通过HTTP请求调用函数。通过 functions.https.onRequest

    我建议你用 onCall 作为函数的端点,并使用 FirebaseFunctions .这样,您就不需要获取FireBaseUser令牌,因为当您使用 firebase函数 .

    记住,typescript只是javascript的超集。我仍将在node.js中给出示例,但建议您在typescript中键入JavaScript代码。

    例子

    index.js(CloudFunctions端点)

    exports.importantfunc = functions.https.onCall((data, context) => {
       // Authentication / user information is automatically added to the request.
       if (!context.auth) {
           // Throwing an HttpsError so that the client gets the error details.
           throw new functions.https.HttpsError('not-authorised', 
                             'The function must be called while authenticated.');
       }
    
       const uid = context.auth.uid;
       const email = context.auth.token.email;
    
       //Do whatever you want
    });
    

    我的片段.java

    //Just some snippets of code examples
    private void callFunction() {
        FirebaseFunctions func = FirebaseFunctions.getInstance();
        func.getHttpsCallable("importantfunc")
                .call()
                .addOnCompleteListener(new OnCompleteListener<HttpsCallableResult>() {
    
                    @Override
                    public void onComplete(@NonNull Task<HttpsCallableResult > task) {
                        if (task.isSuccessful()) {
                             //Success
                        } else {
                             //Failed
                        }
                    }
                });
    }
    

    有关可调用函数的详细信息, read here