简体   繁体   中英

Android - Facebook SDK 3 - Wall post only

I am new to Facebook SDK (3) and have succesfully compiled and run the HelloFacebookSample. Now I want to only publish a wall post so I triedy to delete all the unnecessary stuff and only have the post status functionality

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    postStatusUpdateButton = (Button) findViewById(R.id.postStatusUpdateButton);
    postStatusUpdateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            onClickPostStatusUpdate();
        }
    });
}

private void onClickPostStatusUpdate() {
    performPublish(PendingAction.POST_STATUS_UPDATE);
}

private void performPublish(PendingAction action) {
    Session session = Session.getActiveSession();
    if (session != null) {
        pendingAction = action;
        if (hasPublishPermission()) {
            // We can do the action right away.
            handlePendingAction();
        } else {
            // We need to reauthorize, then complete the action when we get called back.
             Session.ReauthorizeRequest reauthRequest = new Session.ReauthorizeRequest(this, PERMISSIONS).
                        setRequestCode(REAUTHORIZE_ACTIVITY).
                        setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            session.reauthorizeForPublish(reauthRequest);
        }
    }
}

However on performPublish session is null.

I ran into this issue myself. The Session is null because the user is not logged in; the user must be logged in before calling reauthorizeRequest or sending a Request.

One approach is to make sure the user clicks the LoginButton and logs in successfully; if they are not logged in when they attempt to publish a status update (session is null) then you should show an AlertDialog telling them to sign in--or better yet, bringing up the sign-in dialog.

LoginButton is a nice convenience tool, but unfortunately Facebook just gives you the choice between using the LoginButton exactly as it is or writing everything from scratch itself. But you can see the important code if you open LoginButton in Eclipse:

Session currentSession = sessionTracker.getSession();
                if (currentSession == null || currentSession.getState().isClosed()) {
                    sessionTracker.setSession(null);
                    Session session = new Session.Builder(context).setApplicationId(applicationId).build();
                    Session.setActiveSession(session);
                    currentSession = session;
                }
                if (!currentSession.isOpened()) {
                    Session.OpenRequest openRequest = null;
                    if (parentFragment != null) {
                        openRequest = new Session.OpenRequest(parentFragment);
                    } else if (context instanceof Activity) {
                        openRequest = new Session.OpenRequest((Activity)context);
                    }

                    if (openRequest != null) {
                        openRequest.setPermissions(permissions);
                        openRequest.setLoginBehavior(loginBehavior);

                        if (SessionAuthorizationType.PUBLISH.equals(authorizationType)) {
                            currentSession.openForPublish(openRequest);
                        } else {
                            currentSession.openForRead(openRequest);
                        }
                    }
                }

This is the code to log in (that is, to bring up the login screen so the user can log in). You can use the SessionTracker yourself, but I think that Facebook may not mean for people to use that (it isn't in the SDK documentation and the package is com.facebook.internal, implying they don't want developers using it)--but you can adjust the code to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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