Facebook SDK 3.0 - 获取Facebook用户ID和访问令牌

时间:2013-02-28 04:53:35

标签: android facebook

我搜索过去两天并没有成功找到从Facebook SDK 3.0获取用户ID和访问令牌的方法 - 本地登录。

我正在关注facebook本地登录 - http://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/

我使用Session.getAccessToken获取访问令牌,我获得了一些访问令牌,但这是无效的。什么是实际程序?我做错了吗?

如何使用Facebook SDK 3.0获取本地登录中的UserId

1 个答案:

答案 0 :(得分:38)

用户ID:

final Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        // If the session is open, make an API call to get user data
        // and define a new callback to handle the response
        Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                // If the response is successful
                if (session == Session.getActiveSession()) {
                    if (user != null) {
                        user_ID = user.getId();//user id
                        profileName = user.getName();//user's profile name
                        userNameView.setText(user.getName());
                    }   
                }   
            }   
        }); 
        Request.executeBatchAsync(request);
    }  

user_ID& profileName是字符串。

用于accessToken:

String token = session.getAccessToken();

已编辑:(2014年1月13日)

用于用户电子邮件(我没有通过在设备或模拟器上运行来检查此代码):

这些只是我的意见,或者你可以称之为建议

setReadPermissions(Arrays.asList("email", ...other permission...));
//by analyzing the links bellow, i think you can set the permission in loginbutton as:
loginButton.setReadPermissions(Arrays.asList("email", ...other permission...));
user.asMap().get("email");

了解更多信息,请参阅: link1link2link3link4

相关问题