我在我的应用程序中发布到 Facebook 粉丝页面,但我收到了我无法弄清楚的错误。
错误是:
java.lang.UnsupportedOperationException:会话:尝试为具有待处理请求的会话请求新权限。
我可以在我的墙上发布数据,但是当我想在我的应用程序的 Facebook 粉丝页面上发布数据时,我收到了上述错误。
代码片段是:
public class FacebookShareFragment extends Fragment {
private ProgressDialog progressDialog;
private UiLifecycleHelper uiHelper;
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions","manage_pages","publish_stream");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(final Session session,
final SessionState state,
final Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.facebook_share_fragment, container, false);
if (savedInstanceState != null) {
pendingPublishReauthorization =
savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false);
}
shareOnFanPage();
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@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);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public 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 onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
if (pendingPublishReauthorization &&
state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
pendingPublishReauthorization = false;
// publishStory();
shareOnFanPage();
}
} else if (state.isClosed()) {
}
}
/**
* Method to publish on the fan page
*/
public void shareOnFanPage(){
Session session = Session.getActiveSession();
if (session != null) {
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest); // AT THIS POINT I AM GETTING THE ERROR.
return;
}
// Show a progress dialog because the batch request could take a while.
progressDialog = ProgressDialog.show(getActivity(), "",
getActivity().getResources().getString(R.string.progress_dialog_text), true);
try {
final RequestBatch requestBatch = new RequestBatch();
requestBatch.add(fbFan());
requestBatch.executeAsync();
}catch(Exception e){
e.printStackTrace();
}
}
}
首先,我能够在我的 FB 墙上执行发布数据但后来根据要求,我添加了新权限“manage_pages”和“publish_stream”,但不知道为什么即使我评论了 publishStory() 方法也会出现错误在我的 FB 墙上发布数据。
我读了很多帖子,但无法弄清楚......