Facebook SDK, GraphUser user is always null - android

No matter what I try, Facebook GraphUser user is alsways null. I am trying the following using Facebook SDK 3.23.0
loginButton
.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
#Override
public void onUserInfoFetched(GraphUser user) {
FragmentFour.this.user = user;
if (user != null) {
Log.d("Name",user.getName());
} else {
Log.d("Name","Not Logged in!!");
}
}
});
I have done with hash key and have successfully experimented Facebook samples in Facebook SDK using own created app id. But whenever I try above code i always get user as null. Please help.

I solved it by myself by adding
loginBtn.setReadPermissions("user_friends");
loginBtn.setFragment(this);

Related

Sing out Facebook firebase auth completely [duplicate]

i´m not able to find any solution that solves my Problem.
I use Firebase and Facebook to sign up to my app.
When I first sign up to my app, i get the following screen:
Facebook First Login-Screen
I sign out from Firebase and Facebook using the following Lines of Code:
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
When I´m trying after that to sign in with Facebook again, I get the following Screen:
Facebook Second Login-Screen
I´m only able to continue with the Account I used before.
I want to come back to the first Screen, where I have to enter E-Mail an Password so I could Sign in with another Facebook Account if I want.
On Stackoverflow I found the following promising Code:
FacebookSdk.sdkInitialize(getApplicationContext());
if (AccessToken.getCurrentAccessToken() != null) {
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
AccessToken.setCurrentAccessToken(null);
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
But this solved also not my Problem.
After that I get the following Screen:
Facebook alternate Second Login-Screen
I would by very thankful for any help and sorry for any English mistakes.
Use below code before you click on facebook login button
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
accessTokenTracker.stopTracking();
DebugLog.infoLog("Token Changed Called");
if (currentAccessToken == null) {
LoginManager.getInstance().logInWithReadPermissions(mActivity, Arrays.asList(permissionList));;
}
}
};
AccessToken.refreshCurrentAccessTokenAsync();
After above code just call login code as mentioned below (use handler to call below code with 1 milliseconds wait time):
LoginManager.getInstance().logInWithReadPermissions(mActivity, Arrays.asList(permissionList));

Android Facebook login always ask for user after logout

I am implementing a Facebook login with Facebook SDK on Android.
compile 'com.facebook.android:facebook-android-sdk:4.+'
I'm logging in with the user as
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
..
}
And for log out, I use my own log out button and log out the user programmatically:
LoginManager.getInstance().logOut();
My question is:
After log out, when I click on Continue with Facebook, the previous user information pops up to Continue as XY. I don't want this. I want to ask for email and password again, every time, if somebody wants to log in after log out. How can I do this?
Actually i found the solution. I changed the login behavior for the FB login button, for this i used:
loginButton.setLoginBehavior(LoginBehavior.WEB_ONLY);
So every time it pops up the WEB view for login button.
I want to ask for email, password again every time if somebody wants
to log in after log out.
try this in your onCreate()
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
updateWithToken(AccessToken.getCurrentAccessToken()); //add this method
Now in updateWithToken() method logout the user from previous session
private void updateWithToken(AccessToken currentAccessToken) {
if (currentAccessToken != null) {
LoginManager.getInstance().logOut();
} else {
}
}
EDIT
if you want to completely disconnect user from facebook login use:
public void disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
To really disconnect from facebook with the android SDk and avoid auto login after you must use
val accessToken = AccessToken.getCurrentAccessToken()
val request = GraphRequest.newDeleteObjectRequest(accessToken, "me/permissions", { response ->
LoginManager.getInstance().logOut() //not really needed i think
})
request.executeAsync()
In response you have a responseCode == 200 if OK

Facebook SDK 4 for Android - how to log out programmatically

Recently, Facebook released SDK 4 with new and cool updates. I tried to switch into SDK4 to use new features, however, I am struggling with the Login feature of Facebook.
So far, to log out Facebook programmatically, I used :
Session session = Session.getActiveSession();
session.closeAndClearTokenInformation();
But SDK4 seems not to support Session anymore, and in official docs, they mention:
There are two ways to implement Facebook login on Android:
LoginButton class - Which provides a button you can add to your UI. It follows the current access token and can log people in and out.
Well, seems there's no way to log out Facebook programmatically except using LoginButton.
Anyone have any idea, please share it here.
You can use LoginManager.getInstance().logOut();, even if you use LoginButton because
This UI element wraps functionality available in the LoginManager.
EDIT:
Just to mention that this works for Facebook SDK v4. I don't know if they will change it in the future.
#as batoutofhell mention, don't forget to put FacebookSdk.sdkInitialize(getApplicationContext()); to initialize the facebook sdk. Please see here for the details.
SDK4, if you want to completely de-couple, make sure you also remove the app from the user's facebook account. This method disconnects the user completely:
public void disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
You can use LoginManager.logOut()
Check out https://developers.facebook.com/docs/reference/android/current/class/LoginManager/
To handle it with the loginButton:
//Check if user is currently logged in
if (AccessToken.getCurrentAccessToken() != null && com.facebook.Profile.getCurrentProfile() != null){
//Logged in so show the login button
fbLogin.setVisibility(View.VISIBLE);
fbLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//log out
LoginManager.getInstance().logOut();
gotoLogin();
}
});
}
You can logout by using LoginManager but you have to use graph request also. I am talking about log out completely so, that next time you can login with different account.
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
SharedPreferences pref = DashBoard.this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
LoginManager.getInstance().logOut();
Intent logoutint = new Intent(DashBoard.this,MainActivity.class);
logoutint.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logoutint);
}
}).executeAsync();
By the help of shared preferences here you can logout completely, and next time you can login with different account.
Frank version kotlin:
fun disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return // already logged out
}
GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/permissions/",
null,
HttpMethod.DELETE,
GraphRequest.Callback {
LoginManager.getInstance().logOut()
}).executeAsync()
}

Link/unlink existing ParseUser to Facebook account in Android

I'm trying to link an existing user to his or her Facebook account using Parse. After logging into through Parse, the user can go to the SettingsActivity and link their Facebook account.
I achieved this by calling ParseUser.logInInBackground and then verified it by checking if ParseUser.getCurrentUser() != null.
In my SettingsActivity, the user can press a 'Connect to Facebook' button, which is supposed to link the account to Facebook, but it's not working. When the user clicks the button, I executed this code below, as per the Parse Android documentation:
mUser = ParseUser.getCurrentUser();
public void onToggleFacebookConnectedClick(View v) {
if (!ParseFacebookUtils.isLinked(mUser)) {
ParseFacebookUtils.link(mUser, this, new SaveCallback() {
#Override
public void done(ParseException ex) {
if (ParseFacebookUtils.isLinked(mUser)) {
Log.d(Application.APPTAG, "Woohoo, user logged in with Facebook!");
}
}
});
} else if(ParseFacebookUtils.isLinked(mUser)) {
ParseFacebookUtils.unlinkInBackground(mUser, new SaveCallback(){
#Override
public void done(ParseException ex) {
if (ex == null) {
Log.d(Application.APPTAG, "The user is no longer associated with their Facebook account.");
}
}
});
}
}
I am getting the error: com.parse.ParseException:java.lang.IllegalArgumentException: Cannot save a ParseUser until it has been signed up. Call SignUp first.
The user has already signed up (not using Facebook), so I am confused as to why I'm am getting this message. How can I resolve this issue?
It turns out I made a silly mistake. I wasn't linking the user correctly because my Facebook Application ID was not set correctly in my Parse App Settings.

How to Open Facebook auth dialog every time?

I am using ParseFacebookUtils to login through facebook,with this code i successfully logged in through Facebook and got all data of user.
But if Facebook native application is installed on my phone facebook auth dialog is not called every time and went in User is null condition. See following code i used :
ParseFacebookUtils.logIn(Arrays.asList(Permissions.Friends.ACTIVITIES),
this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if(user == null){
System.out.println(" Here");
}else if(user.isNew()){
}else{
}
But i want to open Facebook auth dialog every time that will return me any ParseUser.
How to achieve this kind of funcationality ?
Note :
I checked my Facebook KeyHash and Facebook AppID is correct.
I used Facebook sdk 3.0 and parse sdk 1.2.3
I dont know how to achieve this IN my application
Try this:
Session session = ParseFacebookUtils.getSession();
if (session != null)
session.closeAndClearTokenInformation();
ParseUser.logOut();
This will finish your Facebook session on curren machine. Next time when you ry login - F login dialog will appear.

Categories

Resources