0
我想認證用戶使用嵌入式瀏覽器與javafx。我可以獲取Twitter提供給我的PIN(字符串),但是當我嘗試創建AccesToken時,它無法正確創建。 (我不能得到我的令牌或tokenSecret,都爲空)。Twitter4j AccesToken與JavaFX瀏覽器
這裏是我的代碼:
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";
我根據自己對twitter4j提供了代碼示例,據我可以告訴我做同樣的事情,只有我得到的字符串不是通過鍵盤inputed我把它直接從瀏覽器...
http://twitter4j.org/en/code-examples.html
基於自己對例7