質問

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

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

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

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

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

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;

Add this in your 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;
        }
    }

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.

Hope this will help others.

他のヒント

From the facebook sdk 3.0 documents you can not give the read and publish permission simultaniously. But once you open the session with read permission later you can update this session to publish permission, by UiLifecycleHelper class. Once the session state changes to SessionState.OPENED_TOKEN_UPDATED means session updated with publish permission. Here is sample code for you hope this help you.

public class Example {
    private static final int REAUTH_ACTIVITY_CODE = 100;
    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions", "publish_stream");
    private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_submit_post);
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);
    publishStory();
}

private Session.StatusCallback callback = new Session.StatusCallback() {

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if (exception != null) {
            new AlertDialog.Builder(SubmitPost.this)
                    .setMessage(exception.getMessage())
                    .setPositiveButton("OK", null).show();
        } else {
            SubmitPost.this.onSessionStateChange(session, state, exception);
        }
    }
};

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

 if (state.isOpened()) {
            if (pendingAnnounce && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
                // Session updated with new permissions so try publishing once more.
                pendingAnnounce = false;
                //call your method here to publish something on facebook 
                publishStory();
            }
        }
    }

private void requestPublishPermissions(Session session) {
    if (session != null) {
        pendingAnnounce = true;
        Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);

        newPermissionsRequest.setRequestCode(REAUTH_ACTIVITY_CODE);
        Session mSession = Session.openActiveSessionFromCache(this);
        mSession.addCallback(callback);
        mSession.requestNewPublishPermissions(newPermissionsRequest);
    }
}

private void publishStory() {

    // Check for publish permissions
    List<String> permissions = this.session.getPermissions();
    if (!permissions.containsAll(Arrays.asList(PERMISSIONS ))) {
        this.requestPublishPermissions(this.session);
        this.is_return = true;
        return;
    }

    Session session = Session.getActiveSession();
    if (session != null) {
        CreateFBPost();
    }

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

    switch (requestCode) {
    case REAUTH_ACTIVITY_CODE:
        Session session = Session.getActiveSession();
        if (session != null) {
            session.onActivityResult(this, requestCode, resultCode, data);
        }
        break;
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top