How to give Facebook permission for accessing user

2019-04-15 18:38发布

问题:

I'm developing an application in which I'm using Facebook login for authentication and stuff. facebook sdk 3.0 needs some permission for accessing user data such as profile picture, emailID,publish_stram and etc. How to give permission in the code for accessing those things. Till now I'm able to get fb_access_token. Here is my code:

Session.openActiveSession(this, true, new Session.StatusCallback() {

            // callback when session changes state
            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
                if (session.isOpened()) {
                    // make request to the /me API
                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {

                                // callback after Graph API response with
                                // user
                                // object
                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {
                                        fb_user_id = user.getId();
                                    }

                                    Session session = Session
                                            .getActiveSession();
                                    if (session.isOpened()) {
                                        access_token = session
                                                .getAccessToken();
                                    }

                                    new postFBData().execute();
                                }
                            });
                }
            }
        });

Taken this code snippet from Facebook samples. How to give permission before accessing access_token from FB?

Any help will be appreciated.

回答1:

Calling Session.openActiveSession will only give you basic permissions (until you've requested additional ones). You also need to separate the read and publish permission requests.

In your case, I would do something like:

Session session = // create a new Session using Session.Builder
Session.OpenRequest openRequest = // create an OpenRequest using Session.OpenRequest
openRequest.setPermissions( READ_PERMISSION_LIST );
session.openForRead(openRequest);
Session.setActiveSession(session);

Then, once you've opened the session,

// check if you already have publish permissions first
if (!Session.getActiveSession.getPermissions.contains("publish_stream")) {
  Session.NewPermissionsRequest permissionRequest = // create a NewPermissionsRequest
  permissionRequest.setPermissions( PUBLISH_PERMISSION_LIST);
  Session.getActiveSession().requestNewPublishPermissions(permissionRequest);
}


回答2:

i think this is how you authorize a certain kind of functionality in the String[]

 fb.authorize(MainActivity.this,new String[] {"publish_stream"}, new DialogListener(){

            @Override
            public void onFacebookError(FacebookError e)
            {
                Toast.makeText(MainActivity.this, "on Facebook error", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onError(DialogError e)
            {
                Toast.makeText(MainActivity.this, "on error", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onComplete(Bundle values)
            {
                updateButtonImage();

            }
            @Override
            public void onCancel()
            {

            }
        });