简体   繁体   中英

How can I login facebook with my own button? (Facebook SDK 4.0)

I want to create my own button to login to Facebook. The functions of the button are:

1/ If users logged in Facebook through Facebook app already, when they click the button in my app, they would be signed in my app too. (It must not force them to login Facebook again).

2/ If users did not logged in, it would show this screen to login. http://a5e2fba00d8bcb729d89839f.javacodegeeks.netdna-cdn.com/wp-content/uploads/2014/12/app_credentials.png

It's seem I can't find any tutorials how to do that in Facebook SKD 4.0 and above. I can't use other tutorials because "session" is deprecated.

Please show me how to do that. Thank you in advance!

If users logged in Facebook through Facebook app already, when they click the button in my app, they would be signed in my app too

If user is logged in with Facebook, send Facebook id and Facebook token to your server, your server can get rest of the details from Facebook, create an account on your server and link it with Facebook (id) account.

If user is logged in with your account then he sign in with Facebook, send your account id, Facebook id and Facebook token to server and link both accounts.

If users did not logged in, it would show this screen to login.

Facebook Login: See below code, for detail click here

   @Override
    public View onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.splash, container, false);

        //Initialize facebook
        FacebookSdk.sdkInitialize(getApplicationContext());


        loginButton = (Button) view.findViewById(R.id.login_button);
        loginButton.setOnClickListener(new OnClickListener() {
                @Override public void onClick(View v) {
                //Login with facebook on clicking custom button
                    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
                }
        });

        //Register a callback
        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // login successful
                    }

                    @Override
                    public void onCancel() {
                         // login cancelled
                    }

                    @Override
                    public void onError(FacebookException exception) {
                         // login error  
                    }
        });

    }

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

Other than this code you have to create a

  1. Facebook App ID See Android Getting Started, Add Facebook App ID
  2. Android Key Hash See
  3. And include Facebook Activity in AndroidManifest.xml

Facebook provides a very detailed guide on how to do this: https://developers.facebook.com/docs/facebook-login/android/v2.3

Essentially you have to first register Login callback:

    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    //app code
                }

                @Override
                public void onCancel() {
                    //app code
                }

                @Override
                public void onError(FacebookException exception) {
                    //app code
                }
            });

Then when you want to log the user in, call:

            LoginManager.getInstance().logInWithPublishPermissions(
                    this,
                    Arrays.asList(PERMISSION));

Of course there are setups both in code and in AndroidManifest.xml to properly set up and initialize Facebook SDK, which are detailed in the above link.

Also check out /samples/HelloFacebookSample sample app included with Facebook Android SDK, you can quite easily adapt it to your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM