1

I am doing facebook integration in my project. but the problem is when i click on the facebook login button it is showing loading and retrun to the main screen.Can anyone tell me what is the problem ?

This is my MainActivity :-

public class MainActivity extends AppCompatActivity {

    private FacebookData mFacebookData;
    private Button mLoginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFacebookData = new FacebookData();
        mLoginButton = (Button) findViewById(R.id.button);

        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callFacebookLogin();
            }
        });
    }

    private void callFacebookLogin() {

        Session session = Session.getActiveSession();
        if (session != null) {
            Log.e("session :", "session already build");


            Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {

                    if (user != null) {

                        String str = user.getName();

                        try {


                            String id = user.getId();
                            String firstname = user.getFirstName();
                            String lastname = user.getLastName();
                            String pic = "http://graph.facebook.com/" + id + "/picture?type=large";
                            Log.e("id....", id);
                            Log.e("firstname....", firstname);

                            mFacebookData.setId(id);
                            mFacebookData.setFirstname(firstname);
                            mFacebookData.setLastname(lastname);
                            mFacebookData.setPicture(pic);


                        } catch (Exception e) {

                            e.printStackTrace();
                        }


                    }


                }
            });
            Request.executeBatchAsync(request);
        } else {

            Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() {
                @Override
                public void call(Session session, SessionState state, Exception exception) {


                    if (session.isOpened()) {
                        if (state == SessionState.OPENED) {

                            Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
                                @Override
                                public void onCompleted(GraphUser user, Response response) {


                                    if (user != null) {

                                        String str = user.getName();


                                    }

                                    try {

                                        String id = user.getId();
                                        String firstname = user.getFirstName();
                                        String lastname = user.getLastName();
                                        String pic = "http://graph.facebook.com/"
                                                + id
                                                + "/picture?type=large";


                                        mFacebookData.setId(id);
                                        mFacebookData.setFirstname(firstname);
                                        mFacebookData.setLastname(lastname);
                                        mFacebookData.setPicture(pic);


                                    } catch (Exception e) {

                                        e.printStackTrace();
                                    }


                                }
                            });

                            Request.executeBatchAsync(request);
                        }

                    }


                }
            });


        }

    }
}

This is my Manifest :-

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

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


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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.facebook.LoginActivity"
            android:screenOrientation="portrait" />

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


    </application>

</manifest>

This is my Logcat :-

09-27 10:35:24.405 20441-20615/com.example.aaa.examplepractise E/ActivityThread: Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider
09-27 10:35:24.582 20441-20441/com.example.aaa.examplepractise E/ActivityThread: Failed to find provider info for com.facebook.katana.provider.PlatformProvider
09-27 10:35:24.583 20441-20441/com.example.aaa.examplepractise E/ActivityThread: Failed to find provider info for com.facebook.wakizashi.provider.PlatformProvider
aarav
  • 53
  • 8

1 Answers1

0

This can happen due to the following reasons:

  1. You are not connected to internet
  2. You have not given permission for internet access ( Manifest.xml)
  3. You have not used a correct hashkey for the app
  4. You did not provide a correct App Id

Try the latest method to do

In your fragment class include this:

private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d("Login", "onSuccess");
                GraphRequest.newMeRequest(
                        loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject me, GraphResponse response) {
                                if (response.getError() != null) {
                                    // handle error
                                } else {
                                    email = me.optString("email");
                                    String id = me.optString("id");
                                    String social_id=me.optString("id");
                                    String name= me.optString("name");
                                    String gender=  me.optString("gender");
                                    boolean verified_user=  me.optBoolean("is_verified");                                   
                                }
                            }
                        }).executeAsync();
            }


            @Override
            public void onCancel () {
                Log.d("Login", "onCancel");
            }

            @Override
            public void onError (FacebookException e){
                Log.d("Login", "onError " + e);

            }
        };

In oncreateView Method include below code:

 btn_facebook = (LoginButton) v.findViewById(R.id.btn_fb);
          btn_facebook.setFragment(this);
          btn_facebook.setReadPermissions(Arrays.asList("public_profile, email,user_friends"));
          btn_facebook.registerCallback(mCallbackManager, mFacebookCallback);

In oncreate method add following code:

 FacebookSdk.sdkInitialize(getContext());
        mCallbackManager = CallbackManager.Factory.create();
        setupTokenTracker();
        setupProfileTracker();

                mTokenTracker.startTracking();
        mProfileTracker.startTracking();

        try {

            PackageInfo info = getActivity().getPackageManager().getPackageInfo("package name here", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                // Log.e("Key", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (Exception ex) {
            StringWriter errors = new StringWriter();
            ex.printStackTrace(new PrintWriter(errors));
            // Log.e("Error", errors.toString());

        }

TokenTracker and PRofileTracker methods:

 private void setupTokenTracker() {
        mTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                // Log.d("VIVZ", "" + currentAccessToken);
            }
        };
    }

    private void setupProfileTracker() {
        mProfileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                // Log.d("VIVZ", "" + currentProfile);

            }
        };
    }

In onaCtivityResult you will get data so add like this :

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Log.e("info", "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
        Bundle bundle = data.getExtras();
        for (String key : bundle.keySet()) {
            Object value = bundle.get(key);
        }      
            mCallbackManager.onActivityResult(requestCode, resultCode, data);

    }

In Gradle file add the following dependency:

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

In xml include button like below:

 <com.facebook.login.widget.LoginButton
                    android:id="@+id/btn_fb"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="1dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:singleLine="true"
                    android:textColor="@color/white"
                    android:textSize="16sp"
                    fb:login_text="@string/facebook"
                    fb:logout_text="@string/facebook" />

In manifest add below permissions and meta data and activity tag:

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<activity
            android:name="com.facebook.FacebookActivity"
            android:label="title_facebook_login" />

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

Add the fragment on activity and execute code. Hope it helps LEt me know If need to ask anything else.

Community
  • 1
  • 1
Preetika Kaur
  • 1,946
  • 2
  • 14
  • 23
  • I am following the above mention step correctly. but in my case when i click on the login button it is showing loading for the few time and returns to the main screen. – aarav Sep 27 '16 at 05:14
  • 1
    com.github.asne.facebook:facebook:3.17.2' You are using old one if You say shall I give you new code that I am using ? Latest is 4.0+ and you are using 3.17. Moreover facebook provides easy step to include social login why there is need to add third party for this – Preetika Kaur Sep 27 '16 at 05:17
  • Yeah sure ! share your code please ! because i am little bit confused. – aarav Sep 27 '16 at 05:25
  • @aarav Giev me 2-3 minutes I am changing my answer – Preetika Kaur Sep 27 '16 at 05:32
  • Yeah sure ! no problem ! when you will be free then you can update the code ! – aarav Sep 27 '16 at 05:38
  • 1
    @aarav check this now – Preetika Kaur Sep 27 '16 at 05:49
  • @ Preetika Kaur Maa'm you are not making the object of the CallbackManager Then how can you use the instance of it.In the Fragment class onCreateView method() .in this line :- btn_facebook.registerCallback(mCallbackManager, mFacebookCallback); – aarav Sep 27 '16 at 06:42
  • 1
    It is there in oncreate just declare the object globally @aarav private CallbackManager mCallbackManager; – Preetika Kaur Sep 27 '16 at 06:57
  • @ Preetika Kaur one thing more some it gives the error Couldn't find the URL. can you tell me what is this problem ? and why this occuring ? – aarav Sep 27 '16 at 08:28
  • http://stackoverflow.com/questions/16955468/socialauth-android-couldnt-find-the-url – Preetika Kaur Sep 27 '16 at 08:30
  • http://stackoverflow.com/questions/32731756/got-this-error-while-trying-to-integrate-facebook-login-for-android-couldnt-fi – Preetika Kaur Sep 27 '16 at 08:30
  • 1
    Check these urls if it helps you and yes 4.0 is not latest one so include the latest one and clean rebuild your project – Preetika Kaur Sep 27 '16 at 08:31