I'm using Facebook SDK in Android. I'm trying to create an app that uses facebook without having facebook SDK reside in some Activity or Fragment. i.e - I have a button that after I click it, I want to allow to share some image.
I got it to open the facebook SDK, ask for permission but it is stuck on Session.OPENING and then I can't request permissions -and thus can't publish.
My code is as follows (I didn't post the variables and helper function as they don't add info). It resided in a class and not inside an activity.
At this time I will call PostToFacebook once (it will try to open the session) and then I will call it again - so it will try to post. I will fix this duplicate later.
public void postToFacebook(byte[] ImageToPost, Activity activity, Bundle savedInstanceState) {
Session session = Session.getActiveSession();
if (session != null){
SessionState state = session.getState();
Log.d("facebook","in PostToFacebook. session: " + state.name());
if (!state.isOpened()) {
Log.d("facebook","Session is not open - aborting publish");
return;
}
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(activity, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", "Name of post");
postParams.putString("caption", "Caption of post [testing]");
postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
postParams.putString("link", "some link");
postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
Log.d("facebook","Callback after onCompleted");
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(MainMirrorActivity.context
.getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainMirrorActivity.context
.getApplicationContext(),
postId,
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
} else {
// open new session
Log.d("facebook","opening a new session");
if (savedInstanceState != null) {
session = Session.restoreSession(activity, null, statusCallback, savedInstanceState);
}
if (session == null) {
session = new Session(activity);
}
Session.setActiveSession(session);
//SessionState state = session.getState();
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
Log.d("facebook","openForRead");
session.openForRead(new Session.OpenRequest(activity).setCallback(statusCallback));
}
if (!session.isOpened() && !session.isClosed()) {
Log.d("facebook","openForRead if session not open and not closed");
session.openForRead(new Session.OpenRequest(activity).setCallback(statusCallback));
} else {
Log.d("facebook","openActiveSession");
Session.openActiveSession(activity, true, statusCallback);
}
}
}
When I'm checking the status it is always stuck on OPENING.
Many Thanks!
P.S - the code seems to almost work, so it might help someone anyway.