parse login through Facebook api 4.0 in android studio

前端 未结 5 1167
小鲜肉
小鲜肉 2020-12-19 03:44

My code is.

 private void facebook() {

 List permissions = Arrays.asList(\"public_profile\", \"email\");

    ParseFacebookUtils.logInWithRead         


        
相关标签:
5条回答
  • 2020-12-19 04:25

    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" />
    
    0 讨论(0)
  • 2020-12-19 04:37

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

    0 讨论(0)
  • 2020-12-19 04:42

    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)

    0 讨论(0)
  • 2020-12-19 04:45

    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

    0 讨论(0)
  • 2020-12-19 04:45

    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 讨论(0)
提交回复
热议问题