Sunday, April 28, 2013

[android help] Facebook sdk3 publish


After reading the FB publish tutorial


I'm trying to implement a simple publish, but in my application the user doesn't have to be logged in via FB. I want to open the loggin if needed and if so share.


And I got the next exception:



04-28 12:15:57.350: E/AndroidRuntime(32241): FATAL EXCEPTION: main
04-28 12:15:57.350: E/AndroidRuntime(32241): java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.
04-28 12:15:57.350: E/AndroidRuntime(32241): at com.facebook.Session.requestNewPermissions(Session.java:968)
04-28 12:15:57.350: E/AndroidRuntime(32241): at com.facebook.Session.requestNewPublishPermissions(Session.java:501)


My code:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
boolean isFacebookResponse =
Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
if (isFacebookResponse) {
shareViaFacebook();
}
}

private void CheckFacebookLogin(){
if(Session.getActiveSession() == null){
logInToFacebook();
}else{
shareViaFacebook();
}
}

private void shareViaFacebook() {
Session session = Session.getActiveSession();

if (session != null){

// Check for publish permissions
List permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}

Bundle postParams = new Bundle();
postParams.putString("name", "Facebook SDK for Android");
postParams.putString("caption", "Build great social apps and get more installs.");
postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
postParams.putString("link", "https://developers.facebook.com/android");
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) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(GA_CATEGORY,
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(getActivity()
.getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity()
.getApplicationContext(),
postId,
Toast.LENGTH_LONG).show();
}
}
};

Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);

RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}

private boolean isSubsetOf(Collection subset, Collection superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}

private void logInToFacebook() {
String app_id = getString(R.string.app_id);

Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

Session session = new Session.Builder(getActivity())
.setApplicationId(app_id)
.build();
session.addCallback(this);
Session.setActiveSession(session);

// Login
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this)
.setCallback(this));
} else {
Session.openActiveSession(getActivity(), true, this);
}
}


The login code works. What I'm doing wrong?



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...