我正在我的应用程序中发布到Facebook粉丝页面,但我收到错误,我无法弄清楚。
错误是:
我可以将数据发布java.lang.UnsupportedOperationException: Session: 已尝试 为具有挂起请求的会话请求新权限。
在我的墙上,但是当我想将数据发布在我的应用程序的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",但不知道为什么我会收到错误,即使我评论了发布故事()方法在我的FB墙上发布数据。
我读了很多帖子,但无法弄清楚...
我相信onCreateView是在onActivityResult之前调用的。在这种情况下,如果您的托管活动在权限请求完成之前被终止(或者如果您启用了"不保留活动"开发选项),则此方法将始终在 onActivityResult 之前调用。
由于您还在onCreateView中调用shareOnFanPage,因此您实际上将在前一个权限请求完成之前发出另一个权限请求(因为当您将其传递给uiLifecycleHelper时,它已在onActivityResult中完成)。
我会将 shareOnFanPage() 调用移动到 onResume,或者干脆完全删除它,因为我认为你不需要它,因为你已经在 onResume 方法中调用 onSessionStateChanged。