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
public class FacebookAuth {
private static FacebookAuth instance;
private OnLoginDataReadyListener mResponseListener;
public static synchronized FacebookAuth getInstance() {
if (instance == null) {
instance = new FacebookAuth();
}
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
*/
public void facebookLogin(Activity activity, OnLoginDataReadyListener listener, CallbackManager callbackManager) {
mResponseListener = listener;
LoginManager.getInstance().logInWithReadPermissions(activity, Arrays.asList("public_profile", "user_friends", "email"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
getUserData();
}
@Override
public void onCancel() {
if (mResponseListener != null) {
mResponseListener.onCanceled();
}
}
@Override
public void onError(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
*/
public void getUserData() {
GraphRequest request = GraphRequest.newMeRequest( AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
if (mResponseListener != null) {
mResponseListener.onLoginDataReady(object);
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "picture.height(200).width(200),cover,location,birthday,first_name,last_name,email,gender,name");
request.setParameters(parameters);
request.executeAsync();
}
public interface OnLoginDataReadyListener {
void onLoginDataReady(JSONObject facebookData);
void onCanceled();
}
}
Once you've implemented the above solution, în your activity create a CallbackManager
CallbackManager mCallbackManager = 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()
@Override
protected void onActivityResult(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]"