簡體   English   中英

使用Firebase的Android Facebook登錄僅在某些設備上無法使用

[英]Android Facebook Login with Firebase does not work in some devices only mine

我已經實現了使用Firebase的Android Facebook登錄,但是它不能在其他設備上使用,只能在我的設備上使用。 我已經在朋友的手機上測試了我的應用程序,但是,facebook登錄不起作用,它只顯示進度對話框,然后什么也沒有。 它甚至沒有崩潰。

有誰知道為什么??? 我已經給他寄了我的apk調試程序,我不知道這是否有幫助。

這是我的代碼

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FacebookAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.Arrays;


public class Login extends AppCompatActivity {
    private static final String REQUEST = "Login.java";
    private EditText age;
    private EditText city;
    private EditText about;
    private Button facebookButton;
    private Switch aSwitch;
    private CallbackManager mCallbackManager;
    private String TAG = "Facebook";
    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        age =   findViewById(R.id.age);
        city =  findViewById(R.id.city);
        about = findViewById(R.id.about);
        aSwitch = findViewById(R.id.switch1);
        facebookButton = findViewById(R.id.fb_btn);
        mCallbackManager = CallbackManager.Factory.create();
        facebookButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String cityName =  city.getText().toString();
                String aboutContent = about.getText().toString();
                String ageContent = age.getText().toString();

                if(ageContent.equals("")) {
                    age.setError("Preencha este campo");
                } else if(cityName.equals("")) {
                    city.setError("Preencha este campo");
                } else if(aboutContent.equals("")) {
                    about.setError("Preencha este campo");
                } else {
                    UserDetails.city = cityName;
                    UserDetails.about = aboutContent;
                    UserDetails.age = ageContent;

                    if(aSwitch.isChecked()) {
                        UserDetails.status = "Host";
                    } else {
                        UserDetails.status = "Inquilino";
                    }

                    LoginManager.getInstance().logInWithReadPermissions(Login.this, Arrays.asList("email", "public_profile"));
                    LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            Log.d(TAG, "facebook:onSuccess:" + loginResult);
                            handleFacebookAccessToken(loginResult.getAccessToken());
                        }

                        @Override
                        public void onCancel() {
                            Log.d(TAG, "facebook:onCancel");
                            // ...
                        }

                        @Override
                        public void onError(FacebookException error) {
                            Log.d(TAG, "facebook:onError", error);
                            // ...
                        }
                    });
                }
            }
        });

        mAuth = FirebaseAuth.getInstance();
    }

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Pass the activity result back to the Facebook SDK
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();

        if(currentUser != null) {
            UserDetails.username = currentUser.getUid();
            Log.d("Logadinho", currentUser.getUid());
            Log.d("Logando","lalalal");
            updateUI(currentUser);
        }
    }

    private void updateUI(FirebaseUser currentUser) {
        //Toast.makeText(this, "You are logged in", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Login.this, MainActivity.class);
        Log.d(REQUEST, "Starting activity");
        startActivity(intent);
        finish();
    }

    private void addUserToDatabase(FirebaseUser user) {
        DatabaseReference ref= FirebaseDatabase.getInstance().getReference().child("usuarios").child(user.getUid());
        ref.child("city").setValue(UserDetails.city);
        ref.child("about").setValue(UserDetails.about);
        ref.child("age").setValue(UserDetails.age);
        ref.child("status").setValue(UserDetails.status);
        ref.child("username").setValue(user.getUid());
        ref.child("name").setValue(user.getDisplayName());
        UserDetails.name = user.getDisplayName();
        UserDetails.username = user.getUid();
    }

    private void handleFacebookAccessToken(AccessToken token) {

        Log.d(TAG, "handleFacebookAccessToken:" + token);

        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            addUserToDatabase(user);
                            updateUI(user);

                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(Login.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            updateUI(null);
                        }

                        // ...
                    }
                });
    }

}

這是我的清單

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.sosqueen.com.sosqueen">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Login"
            android:theme="@style/AppFullScreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Chat" />
        <activity android:name=".MainActivity"/>

        <meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>

    </application>

    <uses-permission android:name="android.permission.INTERNET"/>

</manifest>

有誰知道為什么會這樣嗎??? 我是Android開發的新手。

私人void PrintHashKey(){

try {
    PackageInfo info = getPackageManager().getPackageInfo(
            "-----------PUT YOUR PACKAGE NAME ------------",
            PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
    }
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}

}

使用此方法並將此密鑰粘貼到Facebook開發人員控制台中。並構建新的apk ..

回答我自己的問題:看來我們必須去找Facebook開發人員,並使我們的應用生效,以使其他用戶登錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM