Not able to get profile details from Facebook on Android - android

I want to get the profile details from Facebook. I am using the latest Facebook SDK. I want to use custom login button to login and get profile details of user.
In onSuccess method i am getting LoginSuccess object . Now i am stuck how i got user profile data . And this code is on facebookButton not on custom button. Please give me some ideas to integrate the SDK via custom button. I go through the facebook documentation but do not got anything useful.
callbackManager = CallbackManager.Factory.create();
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
}
}
);

You can get simple user info (like name and id) by using in onSuccess() loginManager method Profile class:
Profile.getCurrentProfile() //get current instance of logged profile
Profile.getCurrentProfile().getId() //get current id
Profile.getCurrentProfile().getName() //get current user name
If you want to fetch more information about user, like email or date of birth, you should to use GraphAPI request. You can read about it in related question answer.
It doesn't matter, if you are using Custom Login button or not. In both situations SDK is using loginManager.

Related

Android Facebook [4.10.0] How capture when user clicks logout button?

I've been searching around for the actual logout event button when logging out of facebook. I saw many people answering with this single line:
LoginManager.getInstance().logOut();
But I'm unsure on how to use that.
I am looking for the trigger action that actually logs a user out of facebook. Currently, I am using the LoginManager from facebook's Android SDK to log in.
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
final AccessToken accessToken = loginResult.getAccessToken();
GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject user, GraphResponse graphResponse) {
ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.facebook_profile_1);
facebook_id = user.optString("id");
facebook_name.setText(user.optString("name"));
facebook_name.setVisibility(View.VISIBLE);
profilePictureView.setProfileId(user.optString("id"));
}
}).executeAsync();
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Facebook Login Cancelled", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), "Facebook Login Error", Toast.LENGTH_LONG).show();
}
});
This works fine when the button is ready for login. I'm able to do whatever I'd like in the onCompleted part of that code.
But since facebook does its own thing with its button and changes the single button from "Log In" to "Log Out", how do I change things when the user logs out. To be clear, The user is able to log out, but I want to change things on the screen when this happens (for example, I want to make the name go away and the picture go away)?
When a user clicks "Log Out", a prompt comes up asking if they're sure they want to log out, then when the user clicks "Yes, I'm sure", I want to control parts of my app at that point.
Any thoughts?
I think you want to handle the logout callback and want to implement some coding when user logout you will find the answer here .

android app integration with facebook review compulsory?

I have written a code to login using facebook button sdk 4.0. I am able to login/logout. There is an activity for user from where user can share their locally saved picture to facebook. Sharing is possible if native facebook app is installed. But not in case facebook is not installed. So I tried to use ShareApi for sharing pictures via web view. But it requires publish actions permission. So I just written following code to get permission. It opens a window where it ask for per ok and cancel with my app name. After pressing ok button it always falls in onCancel case automatically.
I have read somewhere that you need to submit your app for review to facebook team in order to take publish actions permission. Is this only reason i am not getting success after trying almost 18 hours.?? Please help me friends.
List<String> permissionNeeds = Arrays.asList("publish_actions");
LoginManager loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(activity,permissionNeeds);
loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess" + loginResult);
sharePhotoToFacebook();
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException error) {
System.out.println("onError"+error);
}
});

Facebook SDK for Android 4.1.0 share dialog cancel

I'm implementing Facebook sharing feature in my app. I took the code from the example https://developers.facebook.com/docs/sharing/android (Share Dialog)
FacebookCallback is implementing 3 methods onSuccess, onCancel, onError.
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Timber.e("onSuccess " + result.getPostId());
}
#Override
public void onCancel() {
Timber.e("onCancel");
}
#Override
public void onError(FacebookException e) {
Timber.e("onError");
}
});
I want to know if user cancels share dialog instead of sharing content. But for some reasons onSuccess is called and result.getPostId() is null in both cases if user successfully shares content or cancels dialog. Why onCancel isn't called if user pressed back and why result.getPostId() is null even if post was shared successfully?
You will only get onCancel if the user has authorized your app (i.e. logged into Facebook via your app).
You will only get the postId if your app also has the publish_actions permission.
See https://developers.facebook.com/docs/android/troubleshooting#onsuccess

Facebook Login on Android App

I have seen numerous stackoverflow posts but none of them seem to be helping me in this problem.
When I try to login using facebook, the facebook app opens, asks for permissions and then when I click OK, everything goes into background. There is nothing in the logs.
Things that I have already tried:
I have created app on Facebook developer console
Added Android as platform on Settings page, added package name, class name and generated key hash. Then i also added the key hash from the logcat error with = at the end.
I have added my email address in contact, and made my app go live in Status & Review.
I have installed and logged in to Facebook app on my android device.
I have tried deleting my debug.keystore, and generating new hash
I have tried deleting Facebook developer app and creating it from scratch
I am using LoginManager to sign-in to facebook from my android app.
Here is my code for login:
private void loginWithFacebook() {
final LoginManager loginManager = LoginManager.getInstance();
CallbackManager callbackManager = CallbackManager.Factory.create();
loginManager.registerCallback(
callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
Log.e("WelcomeScreen", "onSuccess");
}
#Override
public void onCancel() {
Log.e("WelcomeScreen", "onCancel");
}
#Override
public void onError(FacebookException e) {
Log.e("WelcomeScreen", "onError");
}
}
);
loginManager.logInWithReadPermissions(this, Arrays.asList("public_profile"));
}
I have already done this in onCreate:
FacebookSdk.sdkInitialize(getApplicationContext());
I figured out the problem. In my manifest, I had specified noHistory to true for the activity where I was expecting the activity result back from Facebook activity, but since the activity is destroyed once it launches another activity, the app was shutting down. Removing the hoHistory resolved the issue.

How can I switch authorized facebook user with Facebook Android SDK?

I have successfully implemented authorization with facebook in my native android app.
Now I have following problem:
User logout from my app and I call facebook sdk logout method. But when user presses login button it automatically redirects back to my app without showing "Already authorized" window, so he cannot switch to another user.
Is that normal behavior or I've made something wrong? How can I solve this issue?
If you would like to switch to another user, you can refer to the "SwitchUserSample" in Facebook SDK for Android.
Here is some example code fyi.
Session currentSession = Session.getActiveSession();
currentSession.closeAndClearTokenInformation();
newSession = new Session.Builder(LoginActivity.this).build();
newSession.openForRead(new Session.OpenRequest(LoginActivity.this)
.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO)
.setCallback(statusCallback));
No that's not default we can switch user. While you are doing Logout you are not clearing credentials properly so such problems arise , so you need to remove credentials perfectly.
You should got through the HackBook code given by Facebook for all features , where you have to save you Facebook object in SessionStore class and handle it accordingly :
private class SessionListener implements AuthListener, LogoutListener {
#Override
public void onAuthSucceed() {
setImageResource(R.drawable.logout_button);
SessionStore.save(mFb, getContext());
}
#Override
public void onAuthFail(String error) {
}
#Override
public void onLogoutBegin() {
}
#Override
public void onLogoutFinish() {
SessionStore.clear(getContext());
setImageResource(R.drawable.login_button);
}
}

Categories

Resources