首页 > 解决方案 > Facebook登录后从会话中获取用户ID

问题描述

我正在使用 Codenameone,并集成了 Facebook 登录并且运行良好,好消息是当我重新加载我的模拟器时,会话始终处于开启状态,我真正想要做的是获取用户信息并将其保存在我的数据库中在用户表中。有没有教程或可以帮助我的东西。谢谢你。

 private void showIfLoggedIn(UserForm form) {
        String token = (String) Storage.getInstance().readObject("token");
        FaceBookAccess.setToken(token);
            final User me = new User();
            try {
                FaceBookAccess.getInstance().getUser("me", me, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        String miNombre = me.getName();
                          String email = me.getEmail();
                        form.getContentPane().removeAll();                            
                        form.add(new Label(miNombre));                          
                        Button buttonLogout = new Button("Logout");
                        buttonLogout.addActionListener((e) -> {
                            facebookLogout(form);
                            showIfNotLoggedIn(form);
                        });
                        EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(50, 50, 0xffff0000), true);
                        URLImage background = URLImage.createToStorage(placeholder, "fbuser.jpg",
                        "https://graph.facebook.com/v2.11/me/picture?access_token=" + token);
                        background.fetch();
                        ScaleImageLabel myPic = new ScaleImageLabel();
                        myPic.setIcon(background);                       
                        form.add(myPic);
                        form.add(email);
                        form.add(buttonLogout);                       
                        form.revalidate();
                    }                       
                });
            } catch (IOException ex) {
                ex.printStackTrace();
                showIfNotLoggedIn(form);
            }
    }

标签: javafacebookcodenameone

解决方案


不推荐使用中的代码,FacebookAccess因为它已经很旧了。它早于原生FacebookConnectAPI。这里有这个示例:https ://www.codenameone.com/blog/building-a-chat-app-with-codename-one-part-3.html

它包括使用图形 API 和令牌数据访问 facebook 的代码。这使用旧的图形 API 版本,因此您可能需要稍微更新语法,但要点保持不变。


推荐阅读