从Facebook的登录获取用户的电子邮件ID设置权限电子邮件、权限、用户、Facebook

由网友(今天小编来设计一篇适合男生用的好听,新潮又时尚,相信小哥哥们)分享简介:我用 Facebook的3.0 SDK 为Android。我要实现的Facebook 登录。我访问喜欢的名字,用户ID用户的基本信息。但我想有机会的用户还可以发送电子邮件。我已经经历了很多博客和论坛走了,但无法弄清楚如何做到这一点。I'm using Facebook 3.0 SDK for android. I h...

我用 Facebook的3.0 SDK 为Android。我要实现的Facebook 登录。我访问喜欢的名字,用户ID用户的基本信息。但我想有机会的用户还可以发送电子邮件。我已经经历了很多博客和论坛走了,但无法弄清楚如何做到这一点。

I'm using Facebook 3.0 SDK for android. I have to implement Facebook log in. I'm accessing user's basic info like name, userID. But I want to have access to email also of the user. I have gone through many blogs and forum but cannot figure out how to do that.

我用的不是 com.facebook.widget.LoginButton 日志我自己的Andr​​oid按钮。如果我使用的Facebook 登录按钮,这是很容易只需要使用这些行:

I'm using my own android button for log in not com.facebook.widget.LoginButton. If I use Facebook log in button it was easy just have to use these lines:

authButton.setReadPermissions(Arrays.asList("basic_info","email"));

不过,我有自己的要求,将不得不去与默认的Andr​​oid按键。以下是我迄今所做的:

But, I have my own requirement, will have to go with default android buttons. Here is what I have done so far:

Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
        this, Arrays.asList("email"));

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLogin = (Button) findViewById(R.id.logIn);
    mLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(final Session session, SessionState state,
                Exception exception) {
            if (session.isOpened()) {
                session.requestNewReadPermissions(newPermissionsRequest);
                // 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) {                         
                                    System.out
                                            .println("Facebook Response: "
                                                    + response.toString());
                                    access_token = session.getAccessToken();
                                    firstName = user.getFirstName();
                                    fb_user_id = user.getId();


                                    System.out
                                            .println("Facebook Access token: "
                                                    + access_token);
                                    System.out.println("First Name:"
                                            + firstName);
                                    System.out.println("FB USER ID: "
                                            + fb_user_id);


                                }
                            }
                        });
            }
        }
    });

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}

我能得到所有这些事情,我打印的日志,但无法从用户配置文件中检索电子邮件。

I'm able to get all those things which I'm printing in log, but was unable to retrieve email from the user profile.

我能做些什么了吗?

任何形式的帮助将是AP preciated。

Any kind of help will be appreciated.

推荐答案

解决了这个问题:这是我做了什么......

Solved it: Here is what I have done...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLogin = (Button) findViewById(R.id.logIn);
    mLogin.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        // Do whatever u want. User has logged in

    } else if (!currentSession.isOpened()) {
        // Ask for username and password
        OpenRequest op = new Session.OpenRequest((Activity) context);

        op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
        op.setCallback(null);

        List<String> permissions = new ArrayList<String>();
        permissions.add("publish_stream");
        permissions.add("user_likes");
        permissions.add("email");
        permissions.add("user_birthday");
        op.setPermissions(permissions);

        Session session = new Builder(MainActivity.this).build();
        Session.setActiveSession(session);
        session.openForPublish(op);
    }
}

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

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Session.getActiveSession() != null)
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);

    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        Session.openActiveSession(this, true, new Session.StatusCallback() {

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

                if (session.isOpened()) {

                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {

                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {

                                        TextView welcome = (TextView) findViewById(R.id.welcome);
                                        welcome.setText("Hello "
                                                + user.getName() + "!");

                                        access_token = session
                                                .getAccessToken();
                                        firstName = user.getFirstName();
                                        fb_user_id = user.getId();

                                        System.out
                                                .println("Facebook Access token: "
                                                        + access_token);
                                        System.out.println("First Name:"
                                                + firstName);
                                        System.out.println("FB USER ID: "
                                                + fb_user_id);

                                    }
                                }
                            });
                }
            }
        });
    }
}

现在我能收到电子邮件的权限与发布许可的其余部分。

Now I'm able to get email permission with rest of the publish permission.

标记这是公认的答案,以便它可以帮助别人。

Marking this as accepted answer so that it can help others.

阅读全文

相关推荐

最新文章