android facebook sdk - set permissions for openAct

2020-02-26 12:30发布

问题:

I am trying to integrate Facebook SDK to an android app. I got the code from Facebook manual. It uses Session.openActiveSession and then request a graph user. How could I request for more permissions without using LoginButton class?

Session.openActiveSession(this, true, new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);

        if (session.isOpened()) {
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        // got user graph
                    } else {
                        // could not get user graph
                    }
                }
            });
        }
    }
});

Thank you.

回答1:

Try this:

mCallback = new Session.StatusCallback() {...}; // the code you already have

Session.OpenRequest request = new Session.OpenRequest(mContext);
request.setPermissions(Arrays.asList("email", "user_birthday"));
request.setCallback(mCallback );

// get active session
Session mFacebookSession = Session.getActiveSession();
if (mFacebookSession == null || mFacebookSession.isClosed()) 
{
    mFacebookSession = new Session(mContext);
    Session.setActiveSession(mFacebookSession);
}
mFacebookSession.openForRead(request);


回答2:

This solves the problem using opensession and extended persmission just once. Facebook SDK 3.5

Session s = new Session(this);
Session.setActiveSession(s);
Session.OpenRequest request = new Session.OpenRequest(this);
request.setPermissions(Arrays.asList("basic_info","email"));
request.setCallback( new Session.StatusCallback() {
         // callback when session changes state
         @Override
         public void call(Session session, SessionState state, Exception exception) {
             if (session.isOpened()) {
                 Request.newMeRequest(session, new Request.GraphUserCallback() {
                         @Override
                         public void onCompleted(GraphUser user, Response response) {
                             if (user != null) {
                                 Toast.makeText(getApplicationContext(), "User email is:"+user.getProperty("email"), Toast.LENGTH_SHORT).show(); }
                             else {
                                 Toast.makeText(getApplicationContext(), "Error User Null", Toast.LENGTH_SHORT).show();
                             }
                         }
                 }).executeAsync();
             } 
         }
}); //end of call;

s.openForRead(request); //now do the request above