How To Get User's Information After Logging In Facebook App And Authorized Your App? [android]
I'm using this for my Facebook log-in and sharing. I'm wondering if instead of opening a WebView that displays the log-in with Facebookis there a way when a User have already insta
Solution 1:
If you will use this guide you will be able to open Facebook app for login
After implementing Facebook auth, initialize Facebook SDK in your Application class or in activity which uses Facebook login
// initialize facebook sdk and app events logger
FacebookSdk.sdkInitialize(getApplicationContext());
Then you can use the class below to login
publicclassFacebookAuth {
privatestaticFacebookAuth instance;
privateOnLoginDataReadyListener mResponseListener;
publicstatic synchronized FacebookAuthgetInstance() {
if (instance == null) {
instance = newFacebookAuth();
}
return instance;
}
/**
* Call if you want the user to login with his facebook account
* @param activity needed to initialize the Facebook LoginManager
* @param listener used to set the login listener
*/publicvoidfacebookLogin(Activity activity, OnLoginDataReadyListener listener, CallbackManager callbackManager) {
mResponseListener = listener;
LoginManager.getInstance().logInWithReadPermissions(activity, Arrays.asList("public_profile", "user_friends", "email"));
LoginManager.getInstance().registerCallback(callbackManager, newFacebookCallback<LoginResult>() {
@OverridepublicvoidonSuccess(LoginResult loginResult) {
getUserData();
}
@OverridepublicvoidonCancel() {
if (mResponseListener != null) {
mResponseListener.onCanceled();
}
}
@OverridepublicvoidonError(FacebookException error) {
if (mResponseListener != null) {
mResponseListener.onCanceled();
}
}
});
}
/**
* Creates an Facebook Graph request witch will grab the user data
* such as name id and picture for now
*/publicvoidgetUserData() {
GraphRequest request = GraphRequest.newMeRequest( AccessToken.getCurrentAccessToken(), newGraphRequest.GraphJSONObjectCallback() {
@OverridepublicvoidonCompleted(JSONObject object, GraphResponse response) {
if (mResponseListener != null) {
mResponseListener.onLoginDataReady(object);
}
}
});
Bundle parameters = newBundle();
parameters.putString("fields", "picture.height(200).width(200),cover,location,birthday,first_name,last_name,email,gender,name");
request.setParameters(parameters);
request.executeAsync();
}
publicinterfaceOnLoginDataReadyListener {
voidonLoginDataReady(JSONObject facebookData);
voidonCanceled();
}
}
Once you've implemented the above solution, în your activity create a CallbackManager
CallbackManagermCallbackManager= CallbackManager.Factory.create();
Then in button click listener you can login your user as following
FacebookAuth.getInstance().facebookLogin(activity, dataReadyListener, mCallbackManager);
And finally in onActivityResult()
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
Hope this will help you ))
Solution 2:
I use the latest Facebook SDK instead and follow these steps. It is important to add onActivityResult
for Facebook login callbackManager
.
Post a Comment for "How To Get User's Information After Logging In Facebook App And Authorized Your App? [android]"