unable to upload photo sometimes in facebook 3.0? - android

I am trying to post image on Facebook using sdk 3.0, but I am facing some problems. If I close the app and reopens the app it always goes in else condition, then it goes in if condition. Do I need to save session or its already stored??
Session mSession = Session.getActiveSession();
if (mSession != null
&& mSession.getPermissions().contains("publish_actions")) {
postPhoto();
} else {
if (mSession == null) {
Log.d("Facebook", "Session null");
mSession = new Session.Builder(this).setApplicationId(
"xxxxxxxxxxxxxxx").build();
Session.setActiveSession(mSession);
}
if (!mSession.isOpened()) {
Log.d("Facebook", "Session not opened");
Session.OpenRequest sessionRequest = new Session.OpenRequest(
this);
sessionRequest.setPermissions(PERMISSIONS);
sessionRequest.setCallback(statuscallback);
sessionRequest
.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mSession.openForPublish(sessionRequest);
}
}
StatusCallback statuscallback = new StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
// TODO Auto-generated method stub
if (session.isOpened()) {
Log.d("Facebook", "Logged in");
postPhoto();
}
if (session.isClosed()) {
Log.d("Facebook", "Logged out");
}
}
};
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
Log.d("Facebook", "onActivity Result");
}
What if I need to logout the user??
I tried the following code onDestroy()
if (mSession.isOpened())
mSession.closeAndClearTokenInformation();
whenever I open the app next time it always throws an error at this line if I use the above code in destroy method
mSession.openForPublish(sessionRequest);
Please help guys. Answers/Suggestion would be highly appreciable.
Thanks

You can use the UiLifecycleHelper to help manage the active session throughout an Activity or Fragment lifecycle. Generally, all the session related data (access tokens, permissions, etc) are cached, and if you create a new Session(), it will use the cached data (unless you call closeAndClearTokenInformation, in which case it clears the cache). I would only call closeAndClearTokenInformation() on an explicit logout from the user, otherwise just call close() in onDestroy().
I would also avoid calling openForPublish. Always do openForRead, and then call session.getPermissions() to check if you have the necessary permissions, and then call requestNewPublishPermissions if you need more.

Related

Android Facebook sdk Stuck after public profile permission popup

I have been working with Facebook SDK for my current android project. I am facing some weird problems during Facebook login for first time, i.e. When i rum my code for the first time it asks for login and once login is successful , a popup appears asks for public profile permission when i agree a loading progress appears on popup endlessly, i cannot even cancel the popup. It does not go back to 'onActivityResult()' method of the calling activity. Ultimately i have to force stop my application.
But once i open my application again on phone now everything works fine and as expected.
I guess it is happening for the first time when application gets registered to facebook.
If i repeat the whole process , i.e. deleting application from facebook, logging out from device's facebook app, and uninstalling and re-installing my application again on phone, i face the same problem.
I am using Facebook SDK 3.5
Here is my code: activity's onCreate(...) method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello " + user.getName() + "!");
}
}
}).executeAsync();
}
}
});
}
onActivityResult(....) method
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("In on Activity Result");
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

Android facebook sdk 3.0.1 using last session

I have the following scenario with facebook sdk 3.0.1. When user first login and chooses "FB login" then the SSO starts, a new session is open and everything works fine. But then, when the user closes the app and start it again - I don't understand how to get the last open session, currently I'm opening a new session and the user sees again the FB progress bar(while it's being connected to FB again, even so the user already approved FB in his last run). Does anybody know how to skip this operation?
Edit 1:
This is how I retrieve the session:
public void tryRetrievFacebookSession() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened())
return;
session = Session.openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
MobliLog.d("SplashScreen", "Inside call() with session with state: " + session.getState());
// onSessionStateChanged(session, state, exception);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session session = Session.getActiveSession();
if (session != null)
session.onActivityResult(this, requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
The session is normally being created or from the LoginButton or with those lines:
session = Session.getActiveSession();
if (session.getState().isClosed())
session = new Session(this);
if (session.isOpened()) {
onAuthenticationEndListener.onSuccessfullAuthentication();
return;
} else {
this.onFacebookAuthenticationEndListener = onAuthenticationEndListener;
Session.setActiveSession(session);
session.openForRead(new Session.OpenRequest(SocNetwksCompatScreen.this).setCallback(null));
return;
}
Information 1:
When I'm doing the first Session session = Session.getActiveSession(); in the logins after the sso authentication, my session has state CLOSED instead of OPENED
Information 2:
I'm using uiHelper and initialize it like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
twitter = SocialPluginsUtils.getTwitterClient();
uiHelper = new UiLifecycleHelper(this, statusCallback);
uiHelper.onCreate(savedInstanceState);
}
Now, I know that after I perform Session.getActiveSession(); the session state should get OPENED and after that OPEN and then the uiHelper's callback should be invoked. In my code the state is CLOSED or CLOSED_LOGIN_FAILED or CREATED(not sure why it's not stable) and the rest doesn't happens
In fact the sessions are being closed every time the user closed the app.
So if Session.getActiveSession() return a null session you only need to call Session.openActiveSession(activity, true, sessionCallback).
If there is a valid token cache this method will use it in order to open a new session without the need for the user to insert any data. Otherwise this will shows the default dialog with the basic permissions.
From what I can see after your edit maybe the problem is related with the way in which you manage the session.
I use this code in my projects:
Session session = Session.getActiveSession();
if (session == null){
Session.openActiveSession(activity, true, sessionCallback);
}
else if (!session.getState().isOpened()){
session.openForRead(new Session.OpenRequest(activity)
.setCallback(sessionCallback));
//this will open the session with basic read permissions
}
else {
//do what you want with the opened session
}
Moreover if you use the UiLifecycleHelper you don't need the two line of code that I suggest to you in the comment, they are already in the method of the helper. But you must be sure to call all the methods of the helper in each related method of the activity (onResume, onPause etc.)
If there isn't a token cache the openActiveSession(activity, true, sessionCallback) will automatically call a new dialog and if the user login with success a new token cache will be available for future uses.
Problem solved. I accidentally called session.closeAndClearTokenInformation(); in the onStop() (yeah, so stupid!)

StatusCallback not called after requestNewReadPermissions then requestNewPublishPermissions

I am developing an Android App that integrates with Facebook. I would like to:
Let the user login with Facebook
Get the user's email address on Facebook (could be a proxied email address, which is fine)
Post to the user's wall/timeline on his/her behalf
Technically, that would be to:
Authenticate the user
Request the email permission
Request the publish_stream permission
1. Authenticate the user
I called Session.openActiveSession() with a Session.StatusCallback (I already checked that there is no active opened session beforehand):
final Session.StatusCallback sessionStatusCallback = new Session.StatusCallback() {
public void call(final Session session, SessionState state, Exception exception) {
// If there is an exception...
if(exception != null)
{
// Handle fail case here.
return;
}
// If session is just opened...
if(state == SessionState.OPENED)
{
// Handle success case here.
return;
}
};
};
// Start Facebook Login.
Session.openActiveSession(activity, true, sessionStatusCallback);
My callback is called after successful login. So far so good.
2. Request the email permission
This is my status callback:
new Session.StatusCallback() {
public void call(final Session session, SessionState state, Exception exception) {
// If there is an exception...
if(exception != null)
{
// Handle fail case here.
return;
}
// If token is just updated...
if(state == SessionState.OPENED_TOKEN_UPDATED)
{
// Handle success case here.
return;
}
};
};
I request the permission with Session.requestNewReadPermissions():
final Session session = Session.getActiveSession();
final static String[] PERMISSION_ARRAY_READ = {"email"};
final List<String> permissionList = Arrays.asList(PERMISSION_ARRAY_READ);
// If all required permissions are available...
if(session.getPermissions().containsAll(permissionList))
{
// Handle success case here.
return;
}
// Request permissions.
session.requestNewReadPermissions(new Session.NewPermissionsRequest(activity, permissionList));
My callback is called after permission is granted. So far so good.
3. Request the publish_stream permission
This is my status callback:
new Session.StatusCallback() {
public void call(final Session session, SessionState state, Exception exception) {
// If there is an exception...
if(exception != null)
{
// Handle fail case here.
return;
}
// If token is just updated...
if(state == SessionState.OPENED_TOKEN_UPDATED)
{
// Handle success case here.
return;
}
};
};
I request the permission with Session.requestNewPublishPermissions():
final Session session = Session.getActiveSession();
final static String[] PERMISSION_ARRAY_PUBLISH = {"publish_stream"};
final List<String> permissionList = Arrays.asList(PERMISSION_ARRAY_PUBLISH);
// If all required permissions are available...
if(session.getPermissions().containsAll(permissionList))
{
// Handle success case here.
return;
}
// Request permissions.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(activity, permissionList));
This time, my callback is not called after permission is granted.
Investigation
Upon further investigation, I found that my callback is triggered by com.facebook.Session#postStateChange(SessionState, SessionState, Exception):
void postStateChange(final SessionState oldState, final SessionState newState, final Exception exception) {
if (oldState == newState && exception == null) {
return;
}
/* ... */
}
Since oldState and newState are equal (both being SessionState.OPENED_TOKEN_UPDATED, my callback is not called.
Question
How can I receive any notification after permission is granted for the 2nd time? Am I supposed to close() the session and re-open it from cache?
Additional info
My Facebook Android SDK 3.0 is download from here, which is stated in Facebook's Getting Started with the Facebook SDK for Android.
This is a bug.
[edit: As Guy points out in comments, this was fixed in 3.0.1, so this workaround is no longer necessary]
The workaround you mention is basically correct, though you do not need to call close. If you are using the single active session, before calling requestNewPublishPermissions() just call:
Session.openActiveSessionFromCache(myContext);
If you are using multiple sessions, you need to initialize a new Session with the TokenCachingStrategy, verify it is in the CREATED_TOKEN_LOADED state, and call openForRead(null);
After doing one of these, requestNewPublishPermissions() should call your notification once it completes.
Working code based on rightparen's answer
Before requesting permission for the 2nd time (i.e. before Session.requestNewPublishPermissions()), do this:
// Re-initialize Facebook session.
session.removeCallback(sessionStatusCallback); // Remove callback from old session.
session = Session.openActiveSessionFromCache(context); // Create new session by re-opening from cache.
session.addCallback(sessionStatusCallback); // Add callback to new session.
Code is still based on Facebook Android SDK 3.0, as in the question.
Another thing I ran into was that my requestCode for the NewPermissionRequest was not being set to the same requestCode that I used to open my Session for Read with, thereby my Session.StatusCallback was never being invoked when the new permissions have been granted.
For instance, in my onActivityResult I have a check for the requestCode and delegate the call accordingly because I have other stuff coming in to this method.
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == FACEBOOK_AUTH_RESULT_CODE) {
Session session = Session.getActiveSession();
if(session != null) {
session.onActivityResult(activity, requestCode, resultCode, data);
}
}
}
I then opened my Session with the following code :
Session.getActiveSession().openForRead(
new Session.OpenRequest(activity).
setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK).
setRequestCode(FACEBOOK_AUTH_RESULT_CODE).
setPermissions(MY_READ_PERMISSIONS));
I then forgot to use the same requestCode when constructing my NewPermissionRequest.
This is what the correct NewPermissionRequest needs to look like :
Session.getActiveSession().requestNewPublishPermissions(
new NewPermissionsRequest(activity, MY_PUBLISH_PERMISSIONS)
.setRequestCode(FACEBOOK_AUTH_RESULT_CODE));

When to request permissions with Facebook's new Android SDK 3.0?

With Facebook's new Android SDK 3.0 (that was released a few days ago), the process of authentication has changed.
So how do you request a read permission such as "friends_hometown"?
The following code is how I am trying to do it - but I'm quite sure it's not the way you should do this:
Version 1:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Session.openActiveSession(this, true, new Session.StatusCallback() { // start Facebook login
#Override
public void call(Session session, SessionState state, Exception exception) { // callback for session state changes
if (session.isOpened()) {
List<String> permissions = new ArrayList<String>();
permissions.add("friends_hometown");
session.requestNewReadPermissions(new Session.NewPermissionsRequest(FBImport.this, permissions));
Request.executeGraphPathRequestAsync(session, "me/friends/?access_token="+session.getAccessToken()+"&fields=id,name,hometown", new Request.Callback() {
...
});
}
}
});
}
Version 2:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = Session.openActiveSession(this, true, fbStatusCallback); // PROBLEM: NO PERMISSIONS YET BUT CALLBACK IS EXECUTED ON OPEN
currentSession = session;
}
if (currentSession != null && !currentSession.isOpened()) {
OpenRequest openRequest = new OpenRequest(this).setCallback(fbStatusCallback); // HERE IT IS OKAY TO EXECUTE THE CALLBACK BECAUSE WE'VE GOT THE PERMISSIONS
if (openRequest != null) {
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
openRequest.setPermissions(Arrays.asList("friends_hometown"));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
currentSession.openForRead(openRequest);
}
}
}
What I'm doing is to request the permission as soon as the session is open - but at this point the code is already starting a Graph API request, thus the permission request comes to late ...
Can't you request a permission at the same time you initialize the session?
I was able to get it to work. It's a modification of your Version 2 sample. The link Jesse provided also helped a ton.
Here is the code I run through when authenticating a user:
private void signInWithFacebook() {
mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
}
}, null, false);
String applicationId = Utility.getMetadataApplicationId(getBaseContext());
mCurrentSession = mSessionTracker.getSession();
if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
mSessionTracker.setSession(null);
Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
mCurrentSession = session;
}
if (!mCurrentSession.isOpened()) {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(SignUpChoices.this);
if (openRequest != null) {
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location"));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mCurrentSession.openForRead(openRequest);
}
}else {
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
Log.w("myConsultant", user.getId() + " " + user.getName() + " " + user.getInnerJSONObject());
}
});
}
}
For testing I ran it through the below code after returning from Facebooks authentication:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
if (mCurrentSession.isOpened()) {
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
Log.w("myConsultant", user.getId() + " " + user.getName() + " " + user.getInnerJSONObject());
}
});
}
}
I solved the same problem by implementing my own Session.openActiveSession() method:
private static Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback, List<String> permissions) {
OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setCallback(callback);
Session session = new Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
I recommend that you read our login tutorial here specifically in step 3. Using the login button that we provide is the most convenient method, (see authButton.setReadPermissions())
EDIT:
To set permissions without using the loginbutton is trickier since you will have to do all the session management by hand. Digging into the source code for the login button, this line of code is probably what you need. It looks like you will need to create your own Session.OpenRequest and set it's attributes such as permissions, audience, and login behavior, then get the current session and call openForRead() on your Session.OpenRequest.
Although the first question was asked few months ago and there is accepted answers, there is another more elegant solution for requesting more permissions from user while authentication.
In addition, Facebook SDK 3.5 was released recently and it would be good to refresh this thread :)
So, the elegant solution comes with this open source library: android-simple-facebook
Setup
Just add next lines in your Activity class:
Define and select permissions you need, like friends_hometown:
Permissions[] permissions = new Permissions[]
{
Permissions.FRIENDS_HOMETOWN,
Permissions.FRIENDS_PHOTOS,
Permissions.PUBLISH_ACTION
};
The great thing here with the permissions, is that you don't need to seperate READ and PUBLISH permissions. You just mention what you need and library will take care for the rest.
Build and define the configuration by putting app_id, namespace and permissions:
SimpleFacebookConfiguration configuration = new SimpleFacebookConfiguration.Builder()
.setAppId("625994234086470")
.setNamespace("sromkuapp")
.setPermissions(permissions)
.build();
And, create SimpleFacebook instance and set this configuration:
SimpleFacebook simpleFacebook = SimpleFacebook.getInstance(Activity);
simpleFacebook.setConfiguration(configuration);
Now, you can run the methods like: login, publish feed/story, invite,…
Login
mSimpleFacebook.login(OnLoginListener);
Logout
mSimpleFacebook.logout(OnLogoutListener);
For more examples and usage check this page: https://github.com/sromku/android-simple-facebook#actions-examples

Android Facebook SDK3.0, session state OPENING

I am trying to use the Facebook SDK 3.0 to retrieve an access token on button press in my android app. I have a generic button in my Activity that is doing the following:
Button facebook = (Button)findViewById(R.id.facebookLoginButton);
facebook.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Session session = new Session(getApplicationContext());
Session.setActiveSession(session);
session.openForRead(new Session.OpenRequest(SignInActivity.this).setCallback(statusCallback));
}
});
And then the callback:
private class FacebookSessionStatusCallback implements Session.StatusCallback {
#Override
public void call(Session session, SessionState state, Exception exception) {
String s=session.getAccessToken();
}
}
Clicking the button prompts me for my permission to access my profile as expected, but this callback is only ever called once with SessionState as "OPENING". The state doesn't change after this.
What am I missing here? My end goal is really just to get an access token once, and I don't really care about persisting the session or using it to log into my app.
You need to override the onActivityResult so that after the credentials are checked against Facebook for Android (or an inline login) the results are handled and the session updated. Add the following code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession()
.onActivityResult(this, requestCode, resultCode, data);
}
#Lucas Jota: Try changing request code. this works for me.
session.openForRead(new Session.OpenRequest(LoginActivity.this).setCallback(statusCallback).setRequestCode(urRequestCode));
Also, assure that your activity doesn't have "single instance" as property.

Categories

Resources