简体   繁体   中英

How to request new facebook permission without logging out

I am integrating facebook login and publish feed in my application. And for that I am using Android Simple Facebook

I implemented the Login flow, and then I was working on the Publish flow. For that I want to do the following:

When the user clicks on the share to facebook button in my app, I don't want to publish directly but I want to get the publish permission so I can publish later (similar to instagram, where they only get permission at first and then they publish when you add the photo).

For that I have done this:

if (mSimpleFacebook.isLogin()) {
    //The user is logged in so we proceed
    if (mSimpleFacebook.getGrantedPermissions().contains(
        Permission.PUBLISH_ACTION.getValue())) {
        hasPermission = true;
    } else {
        //we don't have permission we request it.
        mSimpleFacebook.requestNewPermissions(
            SocialUtilities.getFacebookPublishPermissions(), true,
            newPermissionListener);
    }
}

However even if I am already logged in and have a session I am prompted to login again before getting permission. I have looked in the code of the Simple Facebook library and it appears that he is logging out and then logging in again, and after login he requests the new permission.

I want to ask, why is it done like that? can't we just request the new permission without logging out then in again (similar to instagram) ? Overriding that behavior in the library is impossible as the SessionManager is private and non accessible.

So is there a way to request new permission without logging out and forcing the user to login again, using the library? Or will I have to put it aside and implement everything from scratch using the native Facebook SDK?

Thank you very much for any help or clarifications on this matter

EDIT: For clarification. in the facebook SDK you can request new permissions, and the user will be presented with the new permission screen to accept. This is similar to instagram And I'm ok with that, that's what I want. But I want to know why the Simple Facebook library is forcing a logout before doing so, is there a way to override that or will I have to stop using that library and reimplement everything by only using the native facebook SDK? Thank

In the latest FB SDK, the Session class has been removed. So this is the equivalent and working code

LoginManager.getInstance().logInWithReadPermissions(
    fragmentOrActivity,
    Arrays.asList("email"));

Ref: https://developers.facebook.com/docs/facebook-login/android/permissions

What you have asked is not possible. (and rightfully so) To obtain new permissions you have to obtain a new user access token. FB will not give you additional permissions without user re-authenticating himself and approving this new permission. This makes a lot of sense - what would be the point of user permissions when any app can get additional permissions without user approval?

Theoretically you do not have to log out before requesting new permissions but this behavior is not supported by FB Android API because there can be only one active session, so this has to be implemented using FB REST API directly.

Probably your best bet is ask for publish permission right at the start.

I am not familiar with android but in php it only revokes the login dialog with the added permission no need to sign-in into facebook. According to this: https://developers.facebook.com/docs/android/login-with-facebook/v2.1#permissions-publish

 Session.NewPermissionsRequest newPermissionsRequest = new Session
   .NewPermissionsRequest(this, Arrays.asList("publish_actions"));
 session.requestNewPublishPermissions(newPermissionsRequest);

One thing that is for sure for each set of permissions you get a new token, from which you can view your permission in open graph debugger. https://developers.facebook.com/tools/debug/ or through the api.

"sorry I couldn't comment that because of my reputation."

this is how i do it!

public class FacebookConnection extends FragmentActivity {
    private MainFragment mainFragment;
    private final String TAG = "FacebookConnection";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Session fbook = Session.openActiveSession(this,false,null);
        if(fbook != null) {
            Log.e(TAG, "!=null");
            if (fbook.isOpened()) {
                Log.e(TAG, "fbook is connected");
                if (fbook.isPermissionGranted("publish_actions") && fbook.isPermissionGranted("user_photos")) {
                    Log.e(TAG, "have permissions");
                    //whatever you want to do.
                    finish();
                }
            }
        }
        if (savedInstanceState == null) {
            // Add the fragment on initial activity setup
            mainFragment = new MainFragment();
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(android.R.id.content, mainFragment)
                    .commit();
        } else {
            // Or set the fragment from restored state info
            mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
        }

    }

and here is the code for the view (its just a button but you can customize to w/e you want!)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.facebook.widget.LoginButton
        android:id="@+id/authButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        />

</LinearLayout>

Now about the multiple permisions you dont have to play with the code for that, go to the facebook developer console, under status and review, submit a review with the permisions you want your app to ask, after facebooks aproves the review, facebook will ask for them with the login from the user.

PD: this works much better than asking for permissions async...

Note: Please use the latest FBandroid API for better options and it is easy to integrate.

If you want publish permission at the beginning use the following code with Facebook LoginButton

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

else use the following permissions to get the login user details

private static final List<String> PERMISSIONS = Arrays.asList("public_profile","user_friends");

Example:(Request any one permission at a time,don't use both)

loginButton.setReadPermissions(PERMISSIONS);

For requesting new permissions use the following

private void requestfor_new_permissions()
{
       Session session = Session.getActiveSession();

        if (session != null)
        {

             // Check whether you have already granted these permissions    
             List<String> permissions = session.getPermissions();

             if (!isSubsetOf(PERMISSIONS, permissions)) 
             {
                 Session.NewPermissionsRequest newPermissionsRequest = new Session
                        .NewPermissionsRequest(this, PERMISSIONS);
                 session.requestNewPublishPermissions(newPermissionsRequest);

                 return;
             }
          else
          {
              //publish your items
          }
        }
}

private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}
private static final String PERMISSION = "publish_actions";
private boolean pendingAnnounce;  

If the session exists. You can check if publsh actions permission is granted already. If it is you can go ahead with your publish details.. else it will ask for permission?

 if (session.isOpened())
     {

                                List<String> permissions = session.getPermissions();
                                if (!permissions.contains(PERMISSION)) {
                                    pendingAnnounce = true;
                                    requestPublishPermissions(session);
                                    return;
                                }



private void requestPublishPermissions(Session session) {
            if (session != null) {
                Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                        this, PERMISSION)
                        // demonstrate how to set an audience for the publish
                        // permissions,
                        // if none are set, this defaults to FRIENDS
                        .setDefaultAudience(SessionDefaultAudience.FRIENDS)
                        .setRequestCode(REAUTH_ACTIVITY_CODE);
                session.requestNewPublishPermissions(newPermissionsRequest);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM