无法添加publish_stream权限

时间:2013-10-26 13:43:22

标签: android facebook

您好我尝试在我的应用中添加public_stream权限。我打算在我的consol中打印我的会话iformation并且我观察到当我添加公共权限时,我的会话状态是CLOSED_LOGIN_FAILED,当我删除此权限时,会在OPENING中设置会话状态并且我已经设置了其他读取权限。

这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.facebook_login_view, container,
            false);

    facebookButton = (LoginButton) view.findViewById(R.id.facebookAuth);
    facebookButton.setFragment(LoginAuthFragment.this);
    //facebookButton.setReadPermissions("email", "user_birthday");
    facebookButton.setPublishPermissions("publish_stream", "email", "user_birthday");

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

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
   System.out.println(session);
   System.out.println("Access token: "+session.getAccessToken());

 final User u = new User();

        if(session.isOpened()){
            System.out.println("OK");
            final String subscribe;
            if(receiveMail.isChecked()){
                subscribe = "1";
            }else{
                subscribe = "0";
            }

            final String access_token=session.getAccessToken();

            Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {

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

                                String firstName = user.getFirstName();
                                String lastName = user.getLastName();
                                String userName = "fb_user_"+user.getId();
                                String facebook_id = user.getId();
                                String birthday = user.getBirthday();
                                String email = user.asMap().get("email")
                                        .toString();
                                String response1 = u.checkFacebookUser(
                                        userName, facebook_id, access_token,
                                        birthday, email, subscribe, firstName, lastName);

                                if (response1.equals("GREEN")) {
                                    prefs.setTokenData(u.getToken(),
                                            u.getCreatedAt(), u.getExpires());
                                    Intent loggedIn = new Intent(getActivity().getApplicationContext(),
                                            MainActivity.class);
                                    startActivity(loggedIn);
                                    getActivity().finish();
                                } else if (response1.equals("RED")) {
                                    Toast.makeText(
                                            getActivity()
                                                    .getApplicationContext(),
                                            "Unknown error", Toast.LENGTH_LONG)
                                            .show();
                                } 

                            }

                }
            });
            Request.executeBatchAsync(request);
        }
}

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

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

@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 onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

这是我添加publish_stream权限时的会话

  

{会话状态:CLOSED_LOGIN_FAILED,令牌:{AccessToken令牌:ACCESS_TOKEN_REMOVED权限:[]},appId:593058290740634}

这是我没有publish_stream权限的会话:

  

{会话状态:已打开,令牌:{AccessToken令牌:ACCESS_TOKEN_REMOVED权限:[email,user_birthday,user_friends,basic_info]},appId:593058290740634}

1 个答案:

答案 0 :(得分:1)

如果没有先获得“basic_info”(这是读取权限),则无法请求发布权限。

正确的做法是首先向用户询问电子邮件和user_birthday权限,当您准备好发布时(通常在一些用户交互之后),然后请求发布流权限。