Facebook login sdk - Android

2019-04-26 11:45发布

问题:

I am trying to connect on my app with the facebook login. I did all these steps and I am facing a strange issue. So the login button works because the facebook login window appears but I am not getting the facebook token (onError is called).

I already tried all the solutions from this Questions.

This code is the same as the one in the Facebook tutorial.

mCallbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = findViewById(R.id.login_button);
    loginButton.setReadPermissions("email");
    loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d(TAG, "facebook:onSuccess:" + loginResult);
        }

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

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

This is my error which is a bit different from the Questions (extra: null):

09-12 21:13:40.317 29573-29573/com.xxxx.yyyy D/FACEBOOK: facebook:onError
SERVER_ERROR: [code] 1675030 [message]: Error performing query. [extra]: null
    at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:219)
    at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:174)
    at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:92)

AndroidManifest.xml

        <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>

In my Build.gradle I added the Facebook SDK

    implementation 'com.facebook.android:facebook-login:[4,5)'

Activity:

public class Home extends AppCompatActivity {

private CallbackManager mCallbackManager;
private static final String TAG = "FACEBOOK";

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

    mCallbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
    loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d(TAG, "facebook:onSuccess:" + loginResult);
        }

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

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


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

}

Does anyone know how to solve this problem?

回答1:

Two things should be done to avoid these problems.

  1. This samples of error come only when an app you are in under development. Follow this link and select your App. Then you can do two things here. From Roles tab, you can allow login to your friends only or else select the App Review and make your app public then anyone can able to log in. And also Refresh Your Tokens.

Go to your panel and select Admin Portal. Step1 and Add tester to your Project.Step2

And also

Incorrect

loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));

Correct one is

loginButton.setReadPermissions("email", "public_profile", "user_friends");



回答2:

The problem may be caused by the status of the application in Facebook Developer Dashboard is under development !

To go live add the privacy policy URL in the Basic settings and Click on Save Changes

EDIT: Change your dependency to

implementation 'com.facebook.android:facebook-android-sdk:4.34.0'

And create Application class as below

import android.app.Application;

import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
    }
}

don't forget declare its in manifest android:name=".MyApplication"