用Linkedin登录一个登录名

时间:2017-03-27 12:37:01

标签: java oauth oauth-2.0 linkedin codenameone

我试图在Codename中使用Linkedin进行登录,但每当我点击登录按钮时,它会显示空白弹出对话框。

enter image description here

以下是我的代码

linked.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {

           Oauth2 auth2 = new Oauth2("https://www.linkedin.com/uas/oauth2/authorization", 
            "XXXXXXXXXX", 
            "https://www.codenameone.com","r_basicprofile", 
            "https://www.linkedin.com/uas/oauth2/accessToken", 
            "XXXXXXXXXX");
   Oauth2.setBackToParent(true);
   auth2.authenticate();
    auth2.createAuthComponent(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent evt) {
                    Log.p(evt.getSource().toString());

            if (evt.getSource() instanceof String) {
                 String token = (String) evt.getSource();
                String expires = Oauth2.getExpires();

                System.out.println("Token=" +token + "Expires in " +expires );
            }
             else {                    
                Exception err = (Exception) evt.getSource();
                err.printStackTrace();
                Dialog.show("Error", "An error occurred while logging in: " + err, "OK", null);
            }
               }
           });

   }
    });

1 个答案:

答案 0 :(得分:1)

在这里,我得到了上述问题的解决方案。

工作代码:

linked.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {


               Oauth2 auth2 = new Oauth2("https://www.linkedin.com/oauth/v2/authorization", 
                "XXXXXXXXXXXXX", 
                "https://www.codenameone.com","r_basicprofile", 
                "https://www.linkedin.com/oauth/v2/accessToken", 
                "XXXXXXXXXXXXX");

       //auth2.authenticate();
        auth2.showAuthentication(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent evt) {
                       AccessToken token = (AccessToken) evt.getSource();
                       JSONObject object = new JSONObject(token);
                       try {
                           TOKEN = object.getString("token");
                           Log.p(TOKEN);
                       } catch (JSONException ex) {
                           ex.printStackTrace();
                       }
                   }
               });
                Oauth2.setBackToParent(true);


       }
        });

上面的代码成功验证了用户链接,作为回应,我们在成功登录TOKEN变量后获得了令牌。