Android不应将读取权限(电子邮件)传递给发布或管理授权的请求

时间:2014-12-31 10:38:50

标签: android facebook facebook-graph-api

我正在为我的Android应用程序使用Facebook SDK。它已成功登录但在此之后我想访问图形api时显示不应将读取权限(电子邮件)传递给发布或管理授权请求并崩溃我的apk与空指针异常。 当我使用SessionLoginBehavior.SUPPRESS_SSO它工作正常,但使用SessionLoginBehavior.SSO_WITH_FALLBACK时显示此错误

以下是我的FacebookService类。

公共类FacebookService {

private Context context;
private Session session;
private Session.StatusCallback statusCallback;
private List<String> permissions = Arrays.asList("email", "user_checkins", "user_birthday", "user_hometown",
        "user_location");
private FacebookRequestListener facebookRequestListener;
private String profile_url;


public interface FacebookRequestListener {

    void signedInFacebookUser(RamblerUser user);

    void errorOccuredOnFacebook();

}

public FacebookService(Context context) {

    this.context = context;

    statusCallback = new SessionStatusCallback();

}

public void login() {

    if (session == null) {
        session = new Session(context);
        Session.setActiveSession(session);
    }

    setupRequest();

}

public void logOut() {
    session = Session.getActiveSession();
    if (!session.isClosed()) {

        session.closeAndClearTokenInformation();

    }

}

public Boolean isSessionAvailable() {

    if (session == null)
        return false;
    else
        return session.isOpened();
}

private void setupRequest() {

    OpenRequest request = new Session.OpenRequest(((Activity) context));

    request.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
    request.setCallback(statusCallback);
    request.setPermissions(permissions);
    session.openForPublish(request);

}

private class SessionStatusCallback implements Session.StatusCallback {

    @Override
    public void call(Session session, SessionState state, Exception exception) {

        if (session.isOpened()) {

            callProfilePictureApi();
        }

    }
}

public void addCallback() {

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

}

public void removeCallback() {

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

public void activityResult(int requestCode, int resultCode, Intent data) {

    Session.getActiveSession().onActivityResult(((Activity) context), requestCode, resultCode, data);

}

public void setFacebookRequestListener(FacebookRequestListener facebookRequestListener) {
    this.facebookRequestListener = facebookRequestListener;
}

private void callProfilePictureApi() {

    Bundle params = new Bundle();
    params.putBoolean("redirect", false);
    params.putString("height", "200");
    params.putString("type", "normal");
    params.putString("width", "200");
    /* make the API call */
    new Request(session, "/me/picture", params, HttpMethod.GET, new Request.Callback() {
        public void onCompleted(Response response) {
            /* handle the result */
            GraphObject go = response.getGraphObject();

            // go.getProperty("url");

            JSONObject obj = go.getInnerJSONObject();

            try {

                profile_url = obj.getJSONObject("data").get("url").toString();

                callProfileApi();

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                facebookRequestListener.errorOccuredOnFacebook();
            }

        }
    }).executeAsync();

}

private void callProfileApi() {
    Request.newMeRequest(session, new Request.GraphUserCallback() {

        @Override
        public void onCompleted(GraphUser user, Response response) {
            // TODO Auto-generated method stub

            if (user != null) {
                String email = user.asMap().get("email").toString();

                // Log.i("profile url", ""+

                RamblerUser rambleUser = new RamblerUser();

                rambleUser.socialId = user.getId();
                rambleUser.address = user.getLocation() != null ? user.getLocation().getName() : "NA";
                rambleUser.email = user.asMap().get("email").toString();
                rambleUser.socialType = com.rambler.Rambler.LoginType.GPlus;
                rambleUser.gender = "NA";
                rambleUser.socilaProfileImageUrl = "NA";
                rambleUser.socilaProfileImageUrl = profile_url;
                rambleUser.name = user.getName();
                rambleUser.screenName = user.getUsername();

                Log.i("email address", email);

                facebookRequestListener.signedInFacebookUser(rambleUser);
            } else {
                facebookRequestListener.errorOccuredOnFacebook();

            }

        }
    }).executeAsync();
}

}

1 个答案:

答案 0 :(得分:0)

使用 ReadPermissions 代替 PublishPermissions 。您正在尝试使用用于发布权限的功能来请求读取权限。