android facebook sdk - 设置openActiveSession的权限

时间:2013-12-13 11:51:16

标签: android facebook session sdk permissions

我正在尝试将Facebook SDK集成到Android应用程序中。我从Facebook手册获得了代码。它使用Session.openActiveSession然后请求图形用户。如何在不使用LoginButton类的情况下请求更多权限?

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

谢谢。

2 个答案:

答案 0 :(得分:17)

试试这个:

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

答案 1 :(得分:7)

这解决了使用opensession和extended persmission一次的问题。 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