我如何顺序获得两个权限?

时间:2012-12-26 08:11:42

标签: android facebook permissions

我正在开发facebook SDK 3.0新的Android应用程序。 我知道facebook的政策,我不能同时请求两个权限,也不能在登录按钮请求写权限(publish_stream,publish_acition..etc)。

所以,我的问题是,如何在用户点击日志记录按钮后自动请求'publish_stream'权限?

在我的源代码中,

private void onSessionStateChange(Session session, SessionState state, Exception exception) {

    if (state.isOpened()) {

        if (pendingPublishReauthorization &&   state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
            pendingPublishReauthorization = false;
            publishFeed(userName);
        }

        m_pd.show();
        Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

            @Override
            public void onCompleted(GraphUser user, Response response) {
                if (user != null) {

                    String username=buildUserInfoDisplay(user);
                    userName=username;
                    saveAccount(username, "facebook");

                    Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "login success");
                    publishFeed(userName);
                    m_pd.dismiss();

                    //Activity GetActivity=getActivity();
                    //if(GetActivity != null) GetActivity.finish();


                }
            }
        });

    } else if (state.isClosed()) {
        Log.i(com.dress.folio.Constants.FACEBOOK_TAG, "Logged out..." + exception);
    }
}

当用户单击登录按钮时,将调用onSessionStateChange()。所以我试图在Request.executeMeRequestAsync(session,new Request.GraphUserCallback()之后用'publishFeed()'请求'publish_stream'。

Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                if (user != null) {

                    String username=buildUserInfoDisplay(user);
                    userName=username;
                    saveAccount(username, "facebook");

                    Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "login success");
                    publishFeed(userName);
                    m_pd.dismiss();

                    //Activity GetActivity=getActivity();
                    //if(GetActivity != null) GetActivity.finish();

                }
            }
        });

publishFeed()就像:

private void publishFeed(String username) {
    Session session = Session.getActiveSession();
    SessionState state= session.getState();

    if (session != null){
        Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "in publishstory(): "+ state);
        // Check for publish permissions    
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions) ) {

            pendingPublishReauthorization = true;
            if(state == SessionState.OPENED_TOKEN_UPDATED) {
                Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "in publishstory() if : OPENED_TOKEN_UPDATED" );
                Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest);
            }

            return;
        }

        Bundle postParams = new Bundle();
        postParams.putString("name", username + " is logged in");
        // postParams.putString("caption", "Build great social apps and get more installs.");
        postParams.putString("description", "get the dress!!");
        postParams.putString("link", "https://www.facebook.com/pages/Dressfolio/183665935091337"); //dressfolio facebook page address
        postParams.putString("picture", "https://MY_APPLICATION.com/asset/facebook/app1/thumb2.png"); //thumbnail


        Request.Callback callback= new Request.Callback() {
            public void onCompleted(Response response) {
                Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "callback()" );

                FacebookRequestError error = response.getError();
                if (error != null) {
                    Log.d(com.dress.folio.Constants.FACEBOOK_TAG,"news feed error!" + error.toString());    
                } 
                else {
                    Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "News Feed Posting success()" );
                }
            }
        };

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

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute(); 


    }


}

但这始终不起作用,因为Sessionstate不处于'OPENED_TOKEN_UPDATED'状态。 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以一次请求多个权限,只是不能在同一请求中混合使用READ和PUBLISH权限。

对于您的特定问题,这是因为您正在检查SessionState.OPENED_TOKEN_UPDATED,而当会话首次打开时,它处于SessionState.OPENED状态。只有在您请求其转换为OPENED_TOKEN_UPDATED状态的其他权限时才会这样做。