获取publish_action许可从Facebook获得读取权限后, [英] Get publish_action permission after getting read permissions from Facebook

查看:883
本文介绍了获取publish_action许可从Facebook获得读取权限后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整合的Facebook 在我的应用程序登录。我能够得到的读取权限,但如何让发布来自Facebook SDK权限。

I'm integrating Facebook log in in my application. I'm able to get the read permissions but how to get publish permissions from Facebook SDK.

这就是我要问的权限读取:

This is how I'm asking for the permissions for read:

@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);
    }
}
});

如果用户授予权限的,我能够获得 access_token ,做我的事情 onActivityResult 。下面是我在做什么与

If user granted the permission I'm able to get access_token and doing my things on onActivityResult. Here is what I'm doing with that:

@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);                                         
                    }
                }
            });
        }
    }
});
}
}

我读 Facebook开发 DOC这里解释的权限 - Facebook的开发者,我不能问的读取权限发布权限。

I have read Facebook Developer doc as explained here Permissions - Facebook Developers, that I cant ask publish permissions with read permissions.

第一个用户将不得不要求读,当用户autorizes的话,我们可以要求发布权限。

First user will have to ask for read and when user autorizes that then we can ask for publish permissions.

怎样才能从Facebook发布权限的用户为授予读取权限后登录。

How can I get publish permissions from Facebook log in after user as granted read permissions.

任何形式的帮助将是AP preciated。

Any kind of help will be appreciated.

推荐答案

我已经得到了回答我的问题。只要有越来越读取权限后打开会话,然后要求发布权限。这是我做了什么:

I have got the answer to my question. Just have to open session after getting read permissions and then ask for publish permissions. Here is what I have done:

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

添加这在 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;
        }
    }

还可以获得这种方法集成在code:

Also get this method integrated in the code:

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

通过做这些事情,我米能够得到读取,以及同时发布的权限。

By doing these things, I"m able to get read as well publish permissions simultaneously.

希望这会帮助别人。

这篇关于获取publish_action许可从Facebook获得读取权限后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆