Hello,
Android applications often require login to identify the user and provide user specific content. Facebook login is one of the popular SDK used to authenticate users.
In this post I will share the code to smoothly re-login the user using Facebook SDK.
Following is complete Login class which can be used for Facebook Login
Android applications often require login to identify the user and provide user specific content. Facebook login is one of the popular SDK used to authenticate users.
In this post I will share the code to smoothly re-login the user using Facebook SDK.
Following is complete Login class which can be used for Facebook Login
public class DemoLoginActivity extends AppCompatActivity { LoginButton loginButton; CallbackManager callbackManager; FacebookCallback<LoginResult> loginResultFacebookCallback = new FacebookCallback<LoginResult>() { @Override public void onSuccess(final LoginResult loginResult) { GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id, name, picture,email"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { } @Override public void onError(FacebookException exception) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); setContentView(R.layout.loginflow); loginButton = (LoginButton) findViewById(R.id.facebook_button); List<String> permissions = new ArrayList<>(); permissions.add("email"); loginButton.setReadPermissions(permissions); callbackManager = CallbackManager.Factory.create(); loginButton.registerCallback(callbackManager, loginResultFacebookCallback); LoginManager.getInstance().registerCallback(callbackManager, loginResultFacebookCallback); AccessToken accessToken = AccessToken.getCurrentAccessToken(); if (accessToken != null) { if (accessToken.isExpired()) { LoginManager.getInstance().logOut(); } else { /// This is code responsible for re-login LoginManager.getInstance().logInWithReadPermissions(this, permissions); } } } protected void onActivityResult(int requestCode, int responseCode, Intent intent) { super.onActivityResult(requestCode, responseCode, intent); callbackManager.onActivityResult(requestCode, responseCode, intent); } } Hope this helps. Happy coding :)
Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.
Keep visiting and sharing.
Thanks,
Ashwani.
0 comments :
Post a Comment