9

My code is.

 private void facebook() {

 List<String> permissions = Arrays.asList("public_profile", "email");

    ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {

        @Override
        public void done(ParseUser user, ParseException err) {
            progressDialog.dismiss();
            Log.e("Facebook", user + "    " + err.getMessage());

            if (user == null) {
                Log.e("MyApp", "Uh oh. The user cancelled the Facebook login.");
            } else if (user.isNew()) {              

                Log.e("MyApp", "User signed up and logged in through Facebook!");
            } else {
                userinfo(user);
                Log.e("MyApp", "User logged in through Facebook!");
            }
        }
    });

}

this code is always provided user is null.. I have use latest Facebook SDK is

compile 'com.facebook.android:facebook-android-sdk:4.0.1'

and Parse is : Parse-1.9.1.jar

and I have use Android Studio. this method is call when click the button this method is work means facebook popup is open but it always provide user is null. This given is user is null but after refresh parseuser is not null.

6
  • Maby the user is null before you pass it to this function. Could you provide the code where you initialize the user?
    – Thealon
    Apr 20, 2015 at 12:58
  • Parse user is initialize an application class.
    – user4291541
    Apr 20, 2015 at 13:08
  • What I've found here: parse.com/docs/android/api/com/parse/ParseUser.html it says "ParseUser() Constructs a new ParseUser with no data in it."
    – Thealon
    Apr 20, 2015 at 13:12
  • ya i know but parse is provide facebook login as a new user.. and I have seen this link but i have not found my answer..
    – user4291541
    Apr 20, 2015 at 13:14
  • I found a solution but he is login a facebook Same problem is my but he is login through parse but how to solve
    – user4291541
    Apr 22, 2015 at 13:07

5 Answers 5

6

Those are the steps I followed to do the signup / login / linking of users with ParseUsers according to Parse documentation and some tips I found on the web. Please check that you didn't miss any step because I had the same problems with you and I had to do all the steps all over again:

  • First, I went to this website and created a new application.
  • I downloaded the Facebook SDK, I did the steps 4 and 5 and I added my package name and the default class name
  • I added the key hashes:

Development Key:

cd C:\Program Files\Java\jdk1.8.0_05\bin keytool -exportcert -alias androiddebugkey -keystore C:\Users\'YOUR NAME'.android\debug.keystore | C:\OpenSSL\bin\openssl sha1 -binary | C:\OpenSSL\bin\openssl base64

Release Key:

cd C:\Program Files\Java\jdk1.8.0_05\bin keytool -exportcert -alias 'NAME YOU GAVE AS ALIAS' -keystore | C:\OpenSSL\bin\openssl sha1 -binary | C:\OpenSSL\bin\openssl base64

  • In parse.com inside Settings and Authentication, I added in Facebook Application the APP ID that was created in Facebook.
  • According to this, I did all the steps until Using Login or Share (including this).
  • I went here and I added my development hash key.
  • In the Settings of the application that I created in Facebook, I checked that in the Key Hashes field, there were both Development and Release Keys.
  • I used the tutorial from the correct answer to make my application live.
  • I downloaded the latest SDK of Parse (1.9.1) from QuickStart because 1.8 it's not working.
  • I added the new .jar file of the SDK and the ParseFacebookUtilsV4-1.9.1 inside the libs folder of my project and syncronized the build.gradle file.
  • I added this line in strings.xml :
    <string name="facebook_app_id">1421******</string>

I added this in my AndroidManifest.xml inside Application tag:

<activity android:name="com.facebook.FacebookActivity"
    android:configChanges=
        "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:label="@string/app_name" />

and this:

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

In build.gradle of the app I added these inside the android because of some errors with Diamond (I am sorry I can't remember the exact error):

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

and I changed the SDK version to be numbers because of some error of undefined variables:

minSdkVersion 14
targetSdkVersion 19

The related dependencies inside the same file:

compile fileTree(include: 'Parse-*.jar', dir: 'libs')
compile fileTree(include: 'ParseFacebookUtilsV4-*.jar', dir: 'libs')
compile 'com.facebook.android:facebook-android-sdk:4.0.0'

  • In the activity in which I want to do all those things with facebook I did the below things:

       Parse.initialize(this, "hoMaK", "wWV193mE");
    
        FacebookSdk.sdkInitialize(getApplicationContext());
    
        ParseFacebookUtils.initialize(getApplicationContext());
    
        final List<String> permissions = Arrays.asList("public_profile", "email");
    
        fb.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                proDialog.show();
    
                ParseFacebookUtils.logInWithReadPermissionsInBackground(HomeActivity.this, permissions, new LogInCallback() {
                    @Override
                    public void done(final ParseUser user, ParseException err) {
                        if (user == null) {
                            Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
    
                            Toast.makeText(getApplicationContext(), "Log-out from Facebook and try again please!", Toast.LENGTH_SHORT).show();
    
                            ParseUser.logOut();
    
                            proDialog.hide();
                        }
                        else if (user.isNew()) {
                            Log.d("MyApp", "User signed up and logged in through Facebook!");
    
                            if (!ParseFacebookUtils.isLinked(user)) {
                                ParseFacebookUtils.linkWithReadPermissionsInBackground(user, HomeActivity.this, permissions, new SaveCallback() {
                                    @Override
                                    public void done(ParseException ex) {
                                        if (ParseFacebookUtils.isLinked(user)) {
                                            Log.d("MyApp", "Woohoo, user logged in with Facebook!");
    
                                            proDialog.hide();
                                        }
                                    }
                                });
                            }
                            else{
                                Toast.makeText(getApplicationContext(), "You can change your personal data in Settings tab!", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Log.d("MyApp", "User logged in through Facebook!");
    
                            if (!ParseFacebookUtils.isLinked(user)) {
                                ParseFacebookUtils.linkWithReadPermissionsInBackground(user, HomeActivity.this, permissions, new SaveCallback() {
                                    @Override
                                    public void done(ParseException ex) {
                                        if (ParseFacebookUtils.isLinked(user)) {
                                            Log.d("MyApp", "Woohoo, user logged in with Facebook!");
    
                                            proDialog.hide();
                                        }
                                    }
                                });
                            }
                            else{
                                proDialog.hide();
                            }
                        }
                    }
                });
            }
        });
    
  • With all the above steps, I can sign up or login using my Facebook account and I am getting a new line in _User object in Parse.com in which I can change the data any time I want to (I handle it as a normal sign-up user)

3
  • You tried at least one time to connect with Facebook and you got an error from Facebook about the hash keys or about the user and after that you are always getting a null user?
    – Marialena
    May 3, 2015 at 11:08
  • when i connect the facebook and login then work correctly . but when login through parsefacebook then user is null in login time.
    – user4291541
    May 4, 2015 at 5:11
  • Have you tried uninstalling your app and the facebook app too and try again?
    – Marialena
    May 4, 2015 at 12:33
4

Don't forget to initialize your the ParseFacebookUtils class in your application class as it is presented in the docs:

ParseFacebookUtils.initialize(this);

Also pass on the onActivityResult to ParseFacebookUtils.

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

Hope that helps ;).

Source: https://parse.com/docs/android/api/com/parse/ParseFacebookUtils.html

4
  • Quite what I said, nice found.
    – Thealon
    Apr 20, 2015 at 13:20
  • 1
    this problem is facing when facebook is update in .. befour update it have properly work..
    – user4291541
    Apr 20, 2015 at 13:22
  • Did you check the facebook key hashes? Is everything ok there? Also check the API key. Apr 20, 2015 at 14:04
  • 1
    I found a solution but he is login a facebook Same problem is my in login in parse but how to solve
    – user4291541
    Apr 22, 2015 at 13:05
2

Not sure but check the Key Hash settings on https://developers.facebook.com for your app. You need to have both key hashes for release and debug version set in app developer console. It might be that you've just set the key hash for the debug environment. For more see Setting a Release Key Hash. Hope it helps.

  ParseFacebookUtils.logInWithReadPermissionsInBackground(this, Arrays.asList("email", "user_photos", "public_profile", "user_friends")
            , new LogInCallback() {
        @Override
        public void done(ParseUser user, ParseException err) {
            if (user == null) {
                Toast.makeText(Form.this,"User Already logged up through Twitter!",Toast.LENGTH_LONG).show();
            } else if (user.isNew()) {


            } else {

                Toast.makeText(Form.this,"User Already logged up through Facebook!",Toast.LENGTH_LONG).show();


            }
        }

    });

Add this also..

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

this code is working perfect..

1
  • All setting is properly .. facebook are login in simple but when it use login through pase that is not work..
    – user4291541
    Apr 29, 2015 at 12:47
1

Let's try:

In your build.gradle

remove this line compile fileTree(dir: 'libs', include: ['*.jar']), then add these:

compile files('libs/Parse-1.9.1.jar')
compile files('libs/ParseFacebookUtilsV4-1.9.1.jar')

that works perfectly for me and with this case :D

0

Have you added Facebook activity into your Manifest?

<activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.