CodeNameOne Google Plus OAuth2

CodeNameOne Google Plus OAuth2,codenameone,Codenameone,有人能帮我用OAuth2认证Google Plus吗?我可以让身份验证窗口显示、登录并用我的帐户确认我的应用程序,但由于某些原因,操作事件从未触发。我得到一个屏幕说,请复制此代码,切换到您的应用程序,并粘贴到那里 如何启动动作事件 提前谢谢 编辑: 我的代码如下: Oauth2 auth2 = new Oauth2("https://accounts.google.com/o/oauth2/auth", "Client_Id", "urn:ietf:wg:oauth:2.0:oob or htt

有人能帮我用OAuth2认证Google Plus吗?我可以让身份验证窗口显示、登录并用我的帐户确认我的应用程序,但由于某些原因,操作事件从未触发。我得到一个屏幕说,请复制此代码,切换到您的应用程序,并粘贴到那里

如何启动动作事件

提前谢谢

编辑:

我的代码如下:

Oauth2 auth2 = new Oauth2("https://accounts.google.com/o/oauth2/auth", "Client_Id", "urn:ietf:wg:oauth:2.0:oob or http://localhost", "openid", "https://accounts.google.com/o/oauth2/token", "Client_Secret");


Oauth2.setBackToParent(true);
auth2.showAuthentication(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() instanceof String) {
            String token = (String) evt.getSource();
            String expires = Oauth2.getExpires();
            GOOGLE_TOKEN = token;
            System.out.println("recived a token " + token + " which expires on " + expires);
            //store token for future queries.
        } else {
            Exception err = (Exception) evt.getSource();
            err.printStackTrace();
            Dialog.show("Error", "An error occurred while logging in: " + err, "OK", null);
        }
    }
});
嗨,Shai,感谢您的回复,我的身份验证代码如下:

Oauth2 auth2 = new Oauth2("https://accounts.google.com/o/oauth2/auth", "Client_Id", "urn:ietf:wg:oauth:2.0:oob or http://localhost", "openid", "https://accounts.google.com/o/oauth2/token", "Client_Secret");


Oauth2.setBackToParent(true);
auth2.showAuthentication(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() instanceof String) {
            String token = (String) evt.getSource();
            String expires = Oauth2.getExpires();
            GOOGLE_TOKEN = token;
            System.out.println("recived a token " + token + " which expires on " + expires);
            //store token for future queries.
        } else {
            Exception err = (Exception) evt.getSource();
            err.printStackTrace();
            Dialog.show("Error", "An error occurred while logging in: " + err, "OK", null);
        }
    }
});

“auth2.showAuthentication”工作得很好,允许用户通过它来授权应用程序,但是一旦用户授权了应用程序,“actionlistener”就不会被调用,我也不会调用回调。我如何强制启动回调以返回令牌?

我一直在为同样的问题而挣扎。我最终发现问题在于
Oauth2.createLoginComponent()
内部的
WebBrowser
设置:
onStart()
处理程序等待以您提供的重定向URI开头的URL,但此URL从未出现。最后一个谷歌页面,即带来令牌的页面,有一个以
开头的URLhttps://accounts.google.com/o/oauth2/approval?“
,不使用重定向URI

据介绍,令牌将在页面标题中提供,但对于代号为One的本机浏览器,情况似乎并非如此。因此,我最终使用
id=“code”
,在HTML文档中创建了一个
input
,它的值就是我们要寻找的标记。必须在加载页面后进行刮削,即在
onLoad()
事件中,而不是在
onStart()


希望这有帮助,它对我有用。

我不清楚你到底想做什么,以及你是如何实现的。如果您提供一些关于如何使用Codename One的附加信息,我认为OAuth2 API可能会对您有所帮助。现在我们将跟进代码是:urn:ietf:wg:oauth:2.0:oob还是您使用的实际字符串?您在注册开发人员密钥时定义了什么?您需要使用该URL,因为google发送的重定向应该将您发送到该位置。我可以同时使用这两种方法,“urn:ietf:wg:oauth:2.0:oob”在完成后在网页上显示auth令牌,但据我所知,“localhost”应该返回并执行回调方法。您应该使用其中之一。其工作方式是OAuth2在完成后向给定页面发送重定向,我们将您提供的地址与我们获得的地址进行比较。所以这需要非常精确,OAuth才能正常工作。