简体   繁体   English

使用Facebook sdk 3.0和Android在墙上发布

[英]publish on wall with Facebook sdk 3.0 and Android

I've read lots of tutorial and answers here, but i'm not capable to implement a button that clicked , publish some text on the wall (with a login done programmatically). 我在这里已经阅读了很多教程和答案,但是我无法实现单击,在墙上发布一些文本(以编程方式完成登录)的按钮。 I tried with a fragment and two buttons, but this code launches a 我尝试了一个片段和两个按钮,但是这段代码启动了一个

05-14 09:26:32.548: E/AndroidRuntime(5080): java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.

the code is: 代码是:

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private static final String TAG = "MainFragment";
private static final String PUBLISH_ACTIONS_PERMISSION = null;
private UiLifecycleHelper uiHelper;
private LoginButton login;
private Button shareButton;


private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.facebook_fragment, container, false);

    shareButton = (Button) view.findViewById(R.id.shareButton);
    login=(LoginButton)view.findViewById(R.id.loginButton);
    login.setPublishPermissions(PERMISSIONS);

    if (savedInstanceState != null) {
        pendingPublishReauthorization = 
            savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false);
    }
    shareButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            publishStory();        
        }
    });
    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()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
    if (pendingPublishReauthorization && 
            state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
        pendingPublishReauthorization = false;
        publishStory();
    }
}

private void publishStory() {
    Session 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 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");

                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(TAG,"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<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
    }
}

where's the problem? 问题出在哪里? i'm going crazy! 我要疯了! Thanks very much! 非常感谢!

I think your problem is that you're explicitly calling onSessionStateChange in your onResume method, where the callback will have called it anyways (via onActivityResult, when your permission request comes back). 我认为您的问题是您在onResume方法中显式调用了onSessionStateChange,无论如何该回调都将调用该方法(当权限请求返回时,通过onActivityResult)。 This results in 2 publish permission requests simultaneously, which is why you're seeing this error. 这导致同时有2个发布权限请求,这就是为什么您看到此错误的原因。 Try removing the onSessionStateChange method in your onResume(). 尝试在onResume()中删除onSessionStateChange方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM