[英]Retrieving access token of OAuth 2.0 (implicit grant) from java Desktop Client using SkyDrive REST API.
我正在嘗試使用SkyDrive REST API從Java桌面客戶端應用程序實現OAuth 2.0隱式授權。 我使用以下代碼:
Desktop.getDesktop().browse(new URL(st.toString()).toURI());
JOptionPane.showMessageDialog(null, "Press ok to continue once you have authenticated.");
我的代碼打開Web瀏覽器並要求用戶登錄,然后SkyDrive以下列形式將訪問令牌發送到瀏覽器URL:
https://login.live.com/oauth20_desktop.srf?lc=1033#access_token=EwAwAq1DBAAUlbRW.....
我想要做的是從我的java程序中讀取此訪問令牌。 我試圖從控制台讀取httpconnection:
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream()));
while(reader.readLine()!=null){
System.out.println(reader.readLine());
但似乎java httpurlconnection不處理javascript響應。 它回復:
<html dir="..... Windows Live ID requires JavaScript to sign in. This web browser either does not support JavaScript, or scripts are being blocked......<body onload="evt_LoginHostMobile_onload(event);">
那么,有沒有辦法直接從java中檢索訪問令牌?
我有同樣的問題。 經過數小時的頭腦風暴,我終於找到了解決方案。 我使用JavaFX庫來創建WebView。 然后你可以攔截位置變化。
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Authenticate extends Application {
static final String APP_ID = "...";
static final String REDIRECT_URL = "https://login.live.com/oauth20_desktop.srf";
static final String RESPONSE_TYPE = "token";
static final String SCOPE = "wl.signin%20wl.offline_access";
private Scene scene;
@Override
public void start(final Stage stage) throws Exception {
final String url = "https://login.live.com/oauth20_authorize.srf?client_id="+APP_ID
+"&scope="+SCOPE+"&response_type="+RESPONSE_TYPE+"&oauth_callback=oob&redirect_uri="+REDIRECT_URL;
BorderPane borderPane = new BorderPane();
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load(url);
borderPane.setCenter(browser);
webEngine.setOnStatusChanged(new EventHandler<WebEvent<String>>() {
public void handle(WebEvent<String> event) {
if (event.getSource() instanceof WebEngine) {
WebEngine we = (WebEngine) event.getSource();
String location = we.getLocation();
if (location.startsWith(REDIRECT_URL) && location.contains("access_token")) {
try {
URL url = new URL(location);
String[] params = url.getRef().split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params) {
String name = param.split("=")[0];
String value = param.split("=")[1];
map.put(name, value);
}
System.out.println("The access token: "+map.get("access_token"));
stage.hide();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
}
});
// create scene
stage.setTitle("Skydrive");
scene = new Scene(borderPane, 750, 500);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.