4

我正在尝试在 facebook 上分享帖子。登录,发布权限第一次正常工作。如果我从 facebook 网络上删除该应用程序,它不会要求我重新安装登录名,并且下面的“代表您发布”发布权限屏幕是我的代码请帮助我犯的错误。我尝试了很多,但工作不正常。谢谢你的好意建议,所以我可以在 android 中改进很多。下面是我的代码...

public class FacebokView extends Activity {
static final String applicationId = String.valueOf(R.string.app_id); 
Session session;

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;
//private StatusCallback statusCallback;

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

    // create the session
    this.session = createSession();
    Log.i("session1--->", "session"+ session.isOpened());

    // start Facebook Login
    //Session.openActiveSession(this, true, new Session.StatusCallback() {
    StatusCallback statusCallback = new StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state, Exception exception) {//READ
            onSessionStateChange(session, state, exception);
            Log.i("OPENED_TOKEN_UPDATED", state.equals(SessionState.OPENED_TOKEN_UPDATED) + "<<test");
            if (session.isOpened() && session.getPermissions().contains("publish_actions")) {
                Log.i("fb session1: publish_actions else if", session.getPermissions().contains("publish_actions") +"OPENED_TOKEN_UPDATED ----"+state.equals(SessionState.OPENED_TOKEN_UPDATED));
                publishStory();
                Log.i("session2--->", "session"+ session.isOpened());

            } else if (session.isOpened()) {
                Log.i("session3--->", "session"+ session.isOpened());
                Log.i("fb session2: publish_actions else if", session.getPermissions().contains("publish_actions") +"OPENED_TOKEN_UPDATED ----"+state.equals(SessionState.OPENED_TOKEN_UPDATED));
                List<String> permissions = session.getPermissions();
                if (!isSubsetOf(PERMISSIONS, permissions)) {
                    pendingPublishReauthorization = true;
                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(FacebookView.this, PERMISSIONS);
                    session.requestNewPublishPermissions(newPermissionsRequest);
                }
            }   else if(session != null && session.getState() == SessionState.CLOSED_LOGIN_FAILED){
                Log.i("fb session:", "not open");
                Log.i("session4--->", "session"+ session.isOpened());
                finish();
            }
        }
    };

    if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)){
        Log.i("session5--->", "session"+ session.isOpened());
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        Log.i("CREATED_TOKEN_LOADED", "session"+ session.isOpened());
    }else if (!session.isOpened() && !session.isClosed()) {
        Log.i("session6--->", "session"+ session.isOpened());
        OpenRequest req = new Session.OpenRequest(this);
        req.setCallback(statusCallback);
        session.openForRead(req);
    } else {
        Log.i("session7--->", "session"+ session.isOpened());
        Session.openActiveSession(this, true, statusCallback);

    }

} 

private void onSessionStateChange(Session session, SessionState state,Exception exception) {
    if (state.isOpened()) {
        if (pendingPublishReauthorization && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
            Log.i("onSessionStateChange--->OPENED_TOKEN_UPDATED -->", state.equals(SessionState.OPENED_TOKEN_UPDATED) + "pendingPublishReauthorization-->" + pendingPublishReauthorization);

            pendingPublishReauthorization = false;
            publishStory();
            Log.i("session12--->", "session"+ session.isOpened());
        }

    } else if (state.isClosed()) {
        Log.i("session8--->", "session"+ session.isOpened());
        //   shareButton.setVisibility(View.INVISIBLE);
    }
}


protected 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;
        }

        // after login make request to the /me API
        Bundle parameters = getIntent().getExtras();
        String  facebookResponse = parameters.getString("facebookResponse");
        Log.i("fbResp from server 1 publish_actions:", session.getPermissions().contains("publish_actions") + "*---facebookResponse----*"+ facebookResponse);

        try {
            JSONObject jsonObj = new JSONObject(facebookResponse);
            parameters.putString("message", jsonObj.getString("MsgForFb"));
            parameters.putString("name", jsonObj.getString("NameForFb"));
            parameters.putString("link", jsonObj.getString("LinkForFb"));
            parameters.putString("Description", jsonObj.getString("DescForFb"));
            parameters.putString("picture", jsonObj.getString("PicForFb"));
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        Request.Callback callback = new Request.Callback() {
            public void onCompleted(Response response) {
                if(response.getGraphObject() != null) {
                    Log.i("fb Response - ", response.toString());
                    JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                    String postId = null;
                    try {
                        postId = graphResponse.getString("id");
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("FacebookView: callBack:", "JSON error : " + e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Toast.makeText(FacebookView.this.getApplicationContext(), error.getErrorMessage(), Toast.LENGTH_LONG).show();
                    } else {
                        Log.i("FacebookView: Request.Callbak: postId = ", " "+postId);
                    }
                }
            }
        };                    

        Request request = new Request(session, "me/feed", parameters, HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
        Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
            // callback after Graph API response with user object
            @Override
            public void onCompleted(GraphUser user, Response response) {
                if (user != null) {
                    finish();
                }
            }
        });
    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}



private Session createSession() {
    Session activeSession = Session.getActiveSession();
    if (activeSession == null || activeSession.getState().isClosed()) {
        activeSession = new Session.Builder(this).setApplicationId(applicationId).build();
        Session.setActiveSession(activeSession);
    }
    return activeSession;
}

private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}
 }
4

1 回答 1

1

如果用户从 Facebook 删除应用程序,您的应用程序将知道的唯一方法是您是否发出图形请求,但它会失败。

以下是一些处理错误的常用策略 - https://developers.facebook.com/docs/reference/api/errors/

您还应该查看 SDK 附带的 Scrumptious 示例,SelectionFragment.java 中有一个 handleError() 方法,它实现了上面链接中概述的策略。

于 2013-07-26T21:35:40.497 回答