I am currently using sessions to log user into Facebook.
private Session createSession() {
Session activeSession = Session.getActiveSession();
if (activeSession == null || activeSession.getState().isClosed()) {
activeSession = new Session.Builder(this).setApplicationId(APP_ID).build();
Session.setActiveSession(activeSession);
}
return activeSession;
}
It is so far simple and straight forward. Is there a way to modify permissions in a similar way, or do i absolutely need to use the Login Button.
to be specific-
I wish to get user notification. So I need extended permission manage_notifications.
I use com.facebook.widget.UserSettingsFragment to manage login/logout to Facebook in my app:
UserSettingsFragment userSettingsFragment = new UserSettingsFragment();
Session session = Session.getActiveSession();
if(session==null || session.isClosed()){
userSettingsFragment.setPublishPermissions(Arrays.asList("publish_actions"));
}
fragmentTransaction.replace(R.id.content_container, userSettingsFragment, "userSettingsFragment");
In another Fragment (but same Activity), Before publishing on Facebook I'm checking whether I'm logged using
Session session = Session.getActiveSession();
if (session != null && session.isOpened()){
publishOnFacebook(intent.getExtras());
}
When I start my app and try to publish, I always get a null Session although I logged in before. And when I start UserSettingsFragment, it shows me that I'm connected.
How can Session.getActiveSession(); be null if I'm actually connected?
Moreover, if I I start the Fragment where I do the publishing straight after UserSettingsFragment, I get the correct Session, i.e.:
1 start the app
2 call `Session.getActiveSession()` in another `Fragment` returns `null`
3 start `UserSettingsFragment` shows that I'm log
but
1 start the app
2 start `UserSettingsFragment` shows that I'm log
3 call `Session.getActiveSession()` in another `Fragment` returns a session
Anybody can help?
Ok, I've just read about openActiveSessionFromCache (not from Facebook doc though):
Session session = Session.getActiveSession();
if(session==null){
// try to restore from cache
session = Session.openActiveSessionFromCache(getActivity());
}
if(session!=null && session.isOpened()){
publishOnFacebook(intent.getExtras());
}
else{
login();
}
Jul's answer does not work for me. So I tried this:
If you had loggedin earlier and if you restarted the app then the session may not be null and it could be in the CREATED_TOKEN_LOADED state. This state implies that the Session has not yet been opened and has a cached token. Opening a Session in this state will not involve user interaction. To open the session from this state you can call the method session.openForRead(null). This immediately opens the session without any user interaction.
So check for this add the following code:
Session session = Session.getActiveSession();
if (session != null){
if(session.getState() == SessionState.CREATED_TOKEN_LOADED){
//this immediately opens the session
session.openForRead(null);
}
if(session.isOpened()) {
publishOnFacebook(intent.getExtras());
}
else{
//start the facebook login fragment or activity
login();
}
}
else{
login();
}
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.
Hi I'm using Facebook sdk3.0 for Android. How can I check whether a user is logged in or not?
Currently I'm using this:
public static boolean isUserLoggedInWithFacebook() {
Session session = Session.getActiveSession();
if(session != null && session.isOpened())
return true;
return false;
}
But it sometimes gives false while user is still logged in.
try using facebook.isSessionValid(). It should return false if the user is logged out.
I'm have a feature on my Android app where the user authorizes the app and shares a link.
I also need to give an option for the user to logout of facebook and I need to conditionally disable this button if the user is not logged int (or not authorized the app).
I can't seem to find the API call on the Android SDK that would let me ask FB if the user is logged in or not.
What I have found is getAccessExpires():
Retrieve the current session's expiration time (in milliseconds since
Unix epoch), or 0 if the session doesn't expire or doesn't exist.
Will checking if the session equals 0 be the way to go? Or is there something I'm missing?
Facebook SDK 4.x versions have a different method now:
boolean loggedIn = AccessToken.getCurrentAccessToken() != null;
or
by using functions
boolean loggedIn;
//...
loggedIn = isFacebookLoggedIn();
//...
public boolean isFacebookLoggedIn(){
return AccessToken.getCurrentAccessToken() != null;
}
Check this link for better reference https://developers.facebook.com/docs/facebook-login/android
check this heading too "Access Tokens and Profiles" it says "You can see if a person is already logged in by checking AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile()
I struggled to find a simple answer to this in the FB docs. Using the Facebook SDK version 3.0 I think there are two ways to check if a user is logged in.
1) Use Session.isOpened()
To use this method you need to retrieve the active session with getActiveSession() and then (here's the confusing part) decipher if the session is in a state where the user is logged in or not. I think the only thing that matters for a logged in user is if the session isOpened(). So if the session is not null and it is open then the user is logged in. In all other cases the user is logged out (keep in mind Session can have states other than opened and closed).
public boolean isLoggedIn() {
Session session = Session.getActiveSession();
return (session != null && session.isOpened());
}
There's another way to write this function, detailed in this answer, but I'm not sure which approach is more clear or "best practice".
2) Constantly monitor status changes with Session.StatusCallback and UiLifecycleHelper
If you follow this tutorial you'll setup the UiLifecycleHelper and register a Session.StatusCallback object with it upon instantiation. There's a callback method, call(), which you override in Session.StatusCallback which will supposedly be called anytime the user logs in/out. Within that method maybe you can keep track of whether the user is logged in or not. Maybe something like this:
private boolean isLoggedIn = false; // by default assume not logged in
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) { //note: I think session.isOpened() is the same
isLoggedIn = true;
} else if (state.isClosed()) {
isLoggedIn = false;
}
}
};
public boolean isLoggedIn() {
return isLoggedIn;
}
I think method 1 is simpler and probably the better choice.
As a side note can anyone shed light on why the tutorial likes to call state.isOpened() instead of session.isOpened() since both seem to be interchangeable (session.isOpened() seems to just call through to the state version anyway).
Note to readers: This is now deprecated in the new FB 3.0 SDK.
facebook.isSessionValid() returns true if user is logged in, false if not.
Session.getActiveSession().isOpened()
returns true if user is logged in, false if not
Android Studio with :
compile 'com.facebook.android:facebook-android-sdk:4.0.1'
then check login like as:
private void facebookPost() {
//check login
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken == null) {
Log.d(TAG, ">>>" + "Signed Out");
} else {
Log.d(TAG, ">>>" + "Signed In");
}
}
#diljeet was right. https://stackoverflow.com/a/29375963/859330
In addition, use
return AccessToken.getAccessToken() != null && Profile.getCurrentProfile()!=null;
It always works this way.
For Facebook Android SDK 4.x you have to use the "AccessToken.getCurrentAccessToken()" as said by #Diljeet but his check didn't work for me, I finally checked it by doing:
Activity "onCreate":
facebookAccessToken = AccessToken.getCurrentAccessToken();
To check if the session is still active (I made it in the "onResume" method but do it where you need):
if(facebookAccessToken != null){
sessionExpired = facebookAccessToken.isExpired();
}else{
sessionExpired = true;
}
More info in https://developers.facebook.com/docs/facebook-login/android
This seems to be working quite well with the new sdk.
private boolean isFacebookLoggedIn(){
Session session = Session.getActiveSession();
if (session != null) {
//Session can be open, check for valid token
if (!session.isClosed()) {
if(!session.getAccessToken().equalsIgnoreCase("")){
return true;
}
}
}
return false;
}
I had the same issue. Here is my solution using SDK 4.0:
First of all, in your activity dealing with login check, be sure to call this primary:
FacebookSdk.sdkInitialize(this.getApplicationContext());
In your onCreate method put this :
updateWithToken(AccessToken.getCurrentAccessToken());
new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
updateWithToken(newAccessToken, handler);
}
};
Then add the method called:
private void updateWithToken(AccessToken currentAccessToken) {
if (currentAccessToken != null) {
fillUIWithFacebookInfos(handler);
} else {
login();
}
}
This way will handle the already logged in user and the newly logged in user.
I was using FB sdk for just for login..
Facebook developers reject my app, Because whenever user login, I send logout.. I dont matter user profile data... I just use FB for login.. FB tells if user login... dont send lgout...
I find Hacking way...
Now my app doesnot send logout whenever user login.... Whatever user login with,Google,Facebook,or normal.. When they click logout... In this three condition
I use
LoginManager.getInstance().logOut();
No matter which platform they use... There is no crash :)