Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

parse login through Facebook api 4.0 in android studio

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 FilesJavajdk1.8.0_05in keytool -exportcert -alias androiddebugkey -keystore C:Users'YOUR NAME'.androiddebug.keystore | C:OpenSSLinopenssl sha1 -binary | C:OpenSSLinopenssl base64

Release Key:

cd C:Program FilesJavajdk1.8.0_05in keytool -exportcert -alias 'NAME YOU GAVE AS ALIAS' -keystore | C:OpenSSLinopenssl sha1 -binary | C:OpenSSLinopenssl 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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...