问题
I want to publish story via Android app and I use a code below.
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private UiLifecycleHelper uiHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(XXActivity.this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.layoutAct);
...
...
...
publishAddCardStory();
...
}
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization);
uiHelper.onSaveInstanceState(outState);
}
private void publishAddCardStory() {
Session session = initFacebookSession(PopupAddCardSuccessActivity.this);
session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle params = new Bundle();
...
params.putString("message", msg);
params.putString("description", desc);
params.putString("link", LINK);
...
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("FB-Response",
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(XXActivity.this
.getApplicationContext(),
getString(R.string.fb_post_fail),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(XXActivity.this
.getApplicationContext(),
getString(R.string.fb_post_success),
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", params,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
//shareButton.setVisibility(View.VISIBLE);
} else if (state.isClosed()) {
//shareButton.setVisibility(View.INVISIBLE);
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private static Session initFacebookSession(Context context) {
Session session = Session.getActiveSession();
if (session != null)
return session;
if (session == null)
session = new Session(context);
return session;
}
Sometime I got these 2 error....
java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.
or sometime...
java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that is not currently open.
Any suggestion would be appreciate.
回答1:
First of all, your initFacebookSession method is useless since you call Session.getActiveSession() right below it, so you can delete this method.
Next, you should handle SessionState changes in onSessionStateChange method. While you don't and only place where you call publishAddCardStory method is in onCreate, I assume the reason you get that errors is this:
- you call publish method in onCreate
- in publish method you call requestNewPermissions over Session, therefore your activity would be destroyed and facebook activity with dialog that requests allowing new permissions from user appears and Session get into pending state
- user would allow (or not) new permissions via dialog
- your activity is created again, therefore you call newPermissionsRequest over Session again while Session is still in pending state while onSessionStateChange was not called yet
Try to look to Facebook Dev Tutorials: https://developers.facebook.com/docs/android/scrumptious/ how to handle SessionState changes. Facebook SDK also provides some sample applications that may be helpful as well. Good luck! :)
来源:https://stackoverflow.com/questions/16932885/session-an-attempt-was-made-to-request-new-permissions-for-a-session-that-has-a