Android java:使用Firebase的匿名登录始终失败

时间:2018-10-04 08:46:04

标签: java android firebase-realtime-database firebase-authentication firebase-storage

我正在与Firebase一起将Firebase实时数据库中的图像加载到RecyclerView中,然后将选定的图像从Firebase Storage下载到手机中。我不希望每个人只有在使用匿名登录方法登录后才能看到图像,所以我制定了如下存储规则:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read : if request.auth != null;
    }
  }
}

数据库规则:

{
  "rules": {
    ".read": true,
    ".write": false
  }
}

我做到了,它在我的第一个应用程序上有效。我想做第二个,所以我复制了所有东西,重命名了数据库名称和存储名称..但是在日志中我得到了:

error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.

E/StorageException: StorageException has occurred.
    User does not have permission to access this object.
     Code: -13021 HttpResult: 403
    Could not open resulting stream.
    java.io.IOException: Could not open resulting stream.

我用来匿名登录的代码:

private void signInAnonymously() {
        mAuth.signInAnonymously().addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
            @Override
            public void onSuccess(AuthResult authResult) {
                Log.e("TAG", "success sign");
                // do your stuff
            }
        })
                .addOnFailureListener(this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Log.e("TAG", "failed sign");
                    }
                });
    }

    @Override
    protected void onStart() {
        super.onStart();
        sign();
    }

    private void sign() {
        mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        if (user != null) {
            Log.e("TAG", "already sign");
        } else {
            signInAnonymously();
            Log.e("TAG", "sign null");
        }
        //loadIntAdd();
    }

我用来下载所选图像的代码:

StorageReference  sr = mStorageRef.child("images/img" +selected_img_num + ".png");
                    sr.getBytes(1024 * 1024).addOnSuccessListener(new OnSuccessListener<byte[]>() {
                        @Override
                        public void onSuccess(byte[] bytes) {
                          //I save the image here
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(final_treatement.this, R.string.oper_failed, Toast.LENGTH_LONG).show();
                            finish();
                        }
                    });

我的依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.firebaseui:firebase-ui-database:0.4.0'
    implementation 'com.google.android.gms:play-services-ads:15.0.1'
    implementation 'com.google.firebase:firebase-storage:16.0.2'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
}

apply plugin: 'com.google.gms.google-services'

我的清单文件:

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/appicon"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@drawable/appicon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".choose_image">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".final_treatement"/>

请注意,我已经在Firebase控制台中激活了匿名登录

我不知道为什么它以前可以工作并且在这里失败

如果有任何遗漏,请回复

2 个答案:

答案 0 :(得分:0)

您收到该错误,是因为您在未经过身份验证的情况下尝试访问文件。由于Firebase存储安全规则设置为仅允许经过身份验证的用户,因此Firebase服务器拒绝您的尝试。要解决此问题,请确保您先经过身份验证,然后获取所需的图像。请记住,根据life-cycle of the activityonCreate()方法在onStart()之前被调用。

答案 1 :(得分:0)

问题可能不在您的代码中,请确保您已从Firebase控制台打开匿名登录 步骤:

  1. 转到firebase控制台中的“身份验证”。
  2. 选择选项卡登录方法
  3. 启用最后一个选项“匿名”
  4. 你们都准备好了:)