通过权限从GraphUser获取电子邮件

时间:2014-08-07 20:00:10

标签: android facebook

我尝试使用下面的代码登录FB。 GraphUser对象具有名字,姓氏,ID,但不包含电子邮件。我知道我需要添加权限,但在这种情况下我该怎么做。权限通常与Session.OpenRequest或LogInButton一起添加,但我没有使用这些权限。有什么建议吗?

Session.openActiveSession(activity, true, new Session.StatusCallback() {
        // callback when session changes state
        @Override
        public void call(final Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                // make request to the /me API

                Request.newMeRequest(session, new Request.GraphUserCallback() {
                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            // get email from GraphUser
                        }
                    }
                }).executeAsync();
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

我有多个权限,然后创建权限列表并调用performPublish()方法(您将传递该权限列表作为参数)以发布权限并检查发布权限。

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

  private void performPublish() {
          Session session = Session.getActiveSession();
          if (session != null) {
              if (hasPublishPermission()) {
                  postStatusUpdate("name");
              } else {
                  session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSIONS));
              }
          }
      }
      private boolean hasPublishPermission() {
          Session session = Session.getActiveSession();
          return session != null && session.getPermissions().contains("publish_actions");
      }

答案 1 :(得分:0)

只需将权限添加为openActiveSession中的参数:

Session.openActiveSession(activity, true, Arrays.asList("public_profile", "email"), new Session.StatusCallback() {
    // callback when session changes state
    @Override
    public void call(final Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            // make request to the /me API

            Request.newMeRequest(session, new Request.GraphUserCallback() {
                // callback after Graph API response with user object
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        // get email from GraphUser
                    }
                }
            }).executeAsync();
        }
    }
});