I want to authentificate a user using an embedded browser with javafx. I can get the PIN (String) that twitter gives me but when I try to create the AccesToken it does not create it correctly. (I can't get my token or tokenSecret, both null).
Here's my code :
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("WebView test");
WebView browser = new WebView();
engine = browser.getEngine();
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
requestToken = twitter.getOAuthRequestToken();
String url = requestToken.getAuthorizationURL();
engine.load(url);
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>()
{
public void changed(ObservableValue ov, State oldState, State newState)
{
if (newState==State.SUCCEEDED && engine.getLocation().equals("https://api.twitter.com/oauth/authorize"))
{
String pin = engine.getDocument().getDocumentElement().getElementsByTagName("code").item(0).getTextContent();
printAccesToken(pin);
}
}
});
StackPane sp = new StackPane();
sp.getChildren().add(browser);
Scene root = new Scene(sp);
primaryStage.setScene(root);
primaryStage.show();
}
private void printAccesToken(String pin)
{
AccessToken accessToken = null;
try
{
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
}
catch (TwitterException e)
{
e.printStackTrace();
}
System.out.println(accessToken.getToken());
}
/*------------------------------------------------------------------*\
|* Attributs Private *|
\*------------------------------------------------------------------*/
private WebEngine engine;
private Twitter twitter;
private RequestToken requestToken;
private static final String CONSUMER_KEY = "xxx";
private static final String CONSUMER_SECRET = "xxx";
I based myself on the code example that twitter4j offers, as far I can tell I am doing the exact same thing, only the String that I get is not inputed via keyboard, I get it directly from the browser ...
http://twitter4j.org/en/code-examples.html
Based myself on Example #7
Twitter API returns the pin in image format. It does not provide pin in string or int. That its. You can prompt user to enter pin manually.