0

I am working on a small JavaFX app that aims to open the Pushbullet authentication Webpage in order to get an OAuth access token. Writing it in JavaFX was really simple. However, when I fill the form and validate it using Google account, authentication fails. Since the same works when I open the URL in my native Chrome browser, I think the issue is related to the limited capability of JavaFX WebEngine implementation but I cannot figure out what it is.

Below is the piece of code I used:

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class GraphicalAuthorizationHelper extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage stage) throws Exception {
        BorderPane borderPane = new BorderPane();

        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();

        borderPane.setCenter(browser);
        webEngine.documentProperty().addListener((prop, oldDoc, newDoc) -> {
            //enableFirebug(webEngine);
        });

        String url = "https://www.pushbullet.com/authorize?client_id=hjT07gVHUYnzN1iVlWIFU7K1Sxype0bf&redirect_uri=http://oauth.yfiton.com/callback&response_type=token";

        webEngine.load(url);

        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        Scene scene = new Scene(
                borderPane,
                primaryScreenBounds.getWidth() * 0.55,
                primaryScreenBounds.getHeight() * 0.65);

        stage.setScene(scene);
        stage.show();
    }

    private static void enableFirebug(final WebEngine engine) {
        engine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
    }

}

I have noticed that the code can be used to get an Oauth access token for Twitter or Slack with success but not with Pushbullet authentication mechanism. When the form is submitted, I get error Error signing in to your account: Could not connect to server:

Pushbullet authentication error

I have sent an email to Pushbullet team about two weeks ago but I got no answer. In the meantime, I have tried to use firebug to understand what could be the issue but without success:

enter image description here

Any idea, comment, etc. is welcome.

Laurent
  • 14,122
  • 13
  • 57
  • 89
  • 1
    Do you have local storage enabled? The site requires that and it's the most common error we see. – Chris Pushbullet Dec 14 '15 at 08:28
  • @ChrisPushbullet Thank you for the comment. Unfortunately, the problem seems not to come from local storage. I am using Java 8 and the documentation says that local storage is implemented with this version of Java. I have also tested this fact by changing the value for variable _url_ (in the piece of code I posted above) to `"http://html5demos.com/storage"`, – Laurent Dec 14 '15 at 19:51
  • 1
    I asked the guy who made that page, and the error "could not connect to server" does appear to mean just that. So the request to the server failed, maybe you can look at the network inspector in firebug to figure out why that is. I have a random theory that it could be related to CORS. The API is at api.pushbullet.com which is a different domain and thus needs CORS to work before it can make requests. – Chris Pushbullet Dec 14 '15 at 22:40

1 Answers1

1

As suggested by Chris Pushbullet, the issue was related to CORS. I succeeded to solve it by setting Java system property sun.net.http.allowRestrictedHeaders to true on the JVM that runs the JavaFX WebEngine instance.

Laurent
  • 14,122
  • 13
  • 57
  • 89