获取Facebook的读取权限后获取publish_action权限

Anu*_*pam 11 android facebook facebook-authentication facebook-android-sdk

我正在将Facebook登录集成到我的应用程序中.我能够获得读取权限,但如何从Facebook SDK获取发布权限.

这就是我要求读取权限的方式:

@Override
public void onClick(View v) {
Session currentSession = Session.getActiveSession();
if (currentSession == null
    || currentSession.getState().isClosed()) {
    Session session = new Session.Builder(context).build();
    Session.setActiveSession(session);
    currentSession = session;
}

if (currentSession.isOpened()) {
    // Do whatever u want. User has logged in

} else if (!currentSession.isOpened()) {
    // Ask for username and password
    OpenRequest op = new Session.OpenRequest((Activity) context);

    op.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
    op.setCallback(null);

    List<String> permissions = new ArrayList<String>();
    permissions.add("user_likes");
    permissions.add("email");
    permissions.add("user_birthday");
    permissions.add("user_location");
    permissions.add("user_interests");
    permissions.add("friends_birthday");
    op.setPermissions(permissions);

    Session session = new Builder(MainActivity.this).build();
    Session.setActiveSession(session);
    session.openForRead(op);
    }
}
});
Run Code Online (Sandbox Code Playgroud)

如果用户授予了我能够获得access_token并执行我的操作的权限onActivityResult.以下是我正在做的事情:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);
if (Session.getActiveSession() != null)
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);

Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
    Session session = new Session.Builder(context).build();
    Session.setActiveSession(session);
    currentSession = session;
}

if (currentSession.isOpened()) {
    Session.openActiveSession(this, true, new Session.StatusCallback() {

    @Override
    public void call(final Session session, SessionState state,
        Exception exception) {

        if (session.isOpened()) {

        Request.executeMeRequestAsync(session,
            new Request.GraphUserCallback() {

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

                    TextView welcome = (TextView) findViewById(R.id.welcome);
                    TextView access_token_txt = (TextView) findViewById(R.id.access_token);
                    TextView email_txt = (TextView) findViewById(R.id.email);                                           

                    access_token = session.getAccessToken();
                    firstName = user.getFirstName();
                    fb_user_id = user.getId();                                          

                    access_token_txt.setText(access_token);

                    JSONObject graphResponse = response
                        .getGraphObject().getInnerJSONObject();

                    fbEmail = null;
                    try {
                        fbEmail = graphResponse.getString("email");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    email_txt.setText(fbEmail);                                         
                    }
                }
            });
        }
    }
});
}
}
Run Code Online (Sandbox Code Playgroud)

我已阅读Facebook Developer文档,如此处所解释的权限 - Facebook开发人员,我不能要求具有读取权限的发布权限.

第一个用户必须要求读取,当用户自动化时,我们可以要求发布权限.

如何在用户获得授予读取权限后从Facebook登录获取发布权限.

任何形式的帮助将不胜感激.

Anu*_*pam 18

我已经得到了我的问题的答案.只需在获得读取权限后打开会话,然后请求发布权限.这是我做的:

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private boolean pendingPublishReauthorization = false;
Run Code Online (Sandbox Code Playgroud)

将此添加到您的onActivityResult:

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

也可以在代码中集成此方法:

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

通过这些操作,我能够同时读取和发布权限.

希望这会有助于他人.