火力的Java / Android的的createUser失败火力、Java、createUser、Android

2023-09-07 12:07:15 作者:酒烈不如她心

我想写的火力地堡用户创建和验证程序一个简单的测试,这样我可以测试自己的火力地堡的安全设置。在code运行平稳,但回调不调用,没有用户的火力地堡端创建。

下面的print语句的输出是:

 开始处理
启动创建用户
创建用户
结束创建用户
结束进程

流程完成出口code 0
 

在code是使用火力点 - 客户端的android-2.2.3.jar文件的火力地堡类的,虽然我只是运行我的测试是在Mac OS Java应用程序。稍后将进入一个Android应用程序,但我希望能够到我的IDE中运行它现在。由经验丰富的火力地堡codeRS任何见解多少AP preciated。

 进口com.firebase.client.Firebase;
进口com.firebase.client.AuthData;
进口com.firebase.client.FirebaseError;

导入的java.util。*;

公共类FireRulesTest {


静态字符串firebase_baseUrl =htt​​ps://开头< myfirebase> .firebaseio.com /;

公共静态无效的主要(字串[] args)
        抛出FirebaseException {

    的System.out.println(开始处理);

    FireRulesTest测试人员=新FireRulesTest();
    tester.createUser();

    的System.out.println(结束进程);
}

私人无效的createUser()
        抛出FirebaseException {

    尝试 {
        的System.out.println(开始创建用户);

        最后弦乐mEmail =me@email.com;
        最后弦乐mPassword =密码;

        最后的火力地堡REF =新的火力地堡(firebase_baseUrl);
        的System.out.println(创建用户);

        ref.createUser(mEmail,mPassword,
                新Firebase.ValueResultHandler<地图<字符串,对象>>(){

                    @覆盖
                    公共无效的onSuccess(地图<字符串,对象>的结果){
                        的System.out.println(成功创建用户帐户UID:+ result.get(UID));
                        ref.authWithPassword(mEmail,mPassword,新Firebase.AuthResultHandler(){
                            @覆盖
                            公共无效onAuthenticated(的authData的authData){
                                //成功,保存身份验证数据
                                HashMap的<字符串,对象> authMap =新的HashMap<字符串,对象>();
                                authMap.put(UID,authData.getUid());
                                authMap.put(令牌,authData.getToken());
                                authMap.put(电子邮件,mEmail);
                                authMap.put(密码,mPassword);

                                火力地堡currentUserRef =新的火力点(firebase_baseUrl +movo /用户/+ authData.getUid());
                                authMap.put(的currentUser,currentUserRef);

                                的System.out.println(用户名:+ authData.getUid()+
                                        ,供应商:+ authData.getProvider()+
                                        ,过期:+ authData.getExpires());


                                的System.out.println(验证完成);
                            }

                            @覆盖
                            公共无效onAuthenticationError(FirebaseError firebaseError){
                                的System.out.println(验证错误验证新创建的用户这可能是一个问题。);
                                的System.out.println(firebaseError.getMessage());
                            }

                        });

                        }

                        @覆盖
                        公共无效onerror的(FirebaseError firebaseError){
                        的System.out.println(上的错误验证新创建的用户这可能是一个问题。);
                            的System.out.println(firebaseError.getMessage());
                        }
            });

            的System.out.println(结束创建用户);

        }赶上(例外FBE){
            的System.out.println(异常:+ fbe.getMessage());
            fbe.printStackTrace();
        }

    }

}
 

解决方案 Eclipse 4.10.0 正式发布,全面拥抱 Java 11

您需要添加一个视频下载在程序结束。可能你的程序退出之前,火力地堡变送东西到服务器的机会。

一个更恰当的解决办法是引入实际生命周期管理为您的应用程序,例如:等待的createUser 调用来完成。但考虑到,你会被这种迁移到Android(用于处理应用程序生命周期反正完全不同),这可能是不值得的努力。

I'm trying to write a simple test of the Firebase user creation and authentication routines so that I can test my Firebase security settings. The code runs smoothly but the callbacks are not invoked and no users are created on the Firebase side.

The output of below with the print statements is:

Begin process
Start Create User
Creating User
End Creating User
End process

Process finished with exit code 0

The code is using the firebase-client-android-2.2.3.jar file for the Firebase classes though I'm just running my test as a java application on a Mac OS. Later this will go into an Android app but I'd like to be able to run it inside my IDE for now. Any insights from experienced Firebase coders much appreciated.

import com.firebase.client.Firebase;
import com.firebase.client.AuthData;
import com.firebase.client.FirebaseError;

import java.util.*;

public class FireRulesTest {


static String firebase_baseUrl = "https://<myfirebase>.firebaseio.com/";

public static void main(String[] args)
        throws FirebaseException {

    System.out.println("Begin process");

    FireRulesTest tester = new FireRulesTest();
    tester.createUser();

    System.out.println("End process");
}

private void createUser()
        throws FirebaseException {

    try {
        System.out.println("Start Create User");

        final String mEmail = "me@email.com";
        final String mPassword = "password";

        final Firebase ref = new Firebase(firebase_baseUrl);
        System.out.println("Creating User");

        ref.createUser(mEmail, mPassword,
                new Firebase.ValueResultHandler<Map<String, Object>>() {

                    @Override
                    public void onSuccess(Map<String, Object> result) {
                        System.out.println("Successfully created user account with uid: " + result.get("uid"));
                        ref.authWithPassword(mEmail, mPassword, new Firebase.AuthResultHandler() {
                            @Override
                            public void onAuthenticated(AuthData authData) {
                                //success, save auth data
                                HashMap<String, Object> authMap = new HashMap<String, Object>();
                                authMap.put("uid", authData.getUid());
                                authMap.put("token", authData.getToken());
                                authMap.put("email", mEmail);
                                authMap.put("password", mPassword);

                                Firebase currentUserRef = new Firebase(firebase_baseUrl + "movo/users/" + authData.getUid());
                                authMap.put("currentUser", currentUserRef);

                                System.out.println("User ID: " + authData.getUid() +
                                        ", Provider: " + authData.getProvider() +
                                        ", Expires:" + authData.getExpires());


                                System.out.println("Authentication complete");
                            }

                            @Override
                            public void onAuthenticationError(FirebaseError firebaseError) {
                                System.out.println("Authentication Error authenticating newly created user. This could be an issue. ");
                                System.out.println(firebaseError.getMessage());
                            }

                        });

                        }

                        @Override
                        public void onError(FirebaseError firebaseError) {
                        System.out.println("On Error authenticating newly created user. This could be an issue. ");
                            System.out.println(firebaseError.getMessage());
                        }
            });

            System.out.println("End Creating User");

        } catch (Exception fbe) {
            System.out.println("Exception: " + fbe.getMessage());
            fbe.printStackTrace();
        }

    }

}

解决方案

You'll want to add a Thread.sleep at the end of the program. Likely your program exits before Firebase gets a chance to send anything to the server.

A more proper solution would be to introduce actual lifecycle management into your app, e.g. waiting for the createUser call to finish. But given that you'll be migrating this to Android (which handles app lifecycle completely different anyway) that might not be worth the effort.