Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SkyDrive REST API从java桌面客户端检索OAuth 2.0(隐式授权)的访问令牌。_Java_Rest_Oauth 2.0_Onedrive - Fatal编程技术网

使用SkyDrive REST API从java桌面客户端检索OAuth 2.0(隐式授权)的访问令牌。

使用SkyDrive REST API从java桌面客户端检索OAuth 2.0(隐式授权)的访问令牌。,java,rest,oauth-2.0,onedrive,Java,Rest,Oauth 2.0,Onedrive,我正在尝试使用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以以下

我正在尝试使用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);">

我也有同样的问题。经过几个小时的头脑风暴,我终于找到了解决办法。我使用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);
    }
}
import java.net.MalformedURLException;
导入java.net.URL;
导入java.util.HashMap;
导入java.util.Map;
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.web.WebEngine;
导入javafx.scene.web.WebEvent;
导入javafx.scene.web.WebView;
导入javafx.stage.stage;
公共类身份验证扩展了应用程序{
静态最终字符串APP_ID=“…”;
静态最终字符串重定向_URL=”https://login.live.com/oauth20_desktop.srf";
静态最终字符串响应\u TYPE=“token”;
静态最终字符串SCOPE=“wl.signin%20wl.offline\u access”;
私密场景;
@凌驾
public void start(final Stage)引发异常{
最终字符串url=”https://login.live.com/oauth20_authorize.srf?client_id=“+APP_ID
+“&scope=“+scope+”&response\u type=“+response\u type+”&oauth\u callback=oob&redirect\u uri=“+redirect\u URL;
BorderPane BorderPane=新的BorderPane();
WebView浏览器=新建WebView();
WebEngine WebEngine=browser.getEngine();
加载(url);
边框窗格。设置中心(浏览器);
setOnStatusChanged(新的EventHandler(){
公共无效句柄(WebEvent事件){
if(event.getSource()instanceof WebEngine){
WebEngine we=(WebEngine)事件.getSource();
字符串位置=we.getLocation();
if(location.startsWith(重定向\u URL)和&location.contains(“访问\u令牌”)){
试一试{
URL=新URL(位置);
字符串[]params=url.getRef().split(&);
Map Map=newhashmap();
for(字符串参数:params){
字符串名称=参数拆分(“=”[0];
字符串值=参数拆分(“=”[1];
map.put(名称、值);
}
System.out.println(“访问令牌:+map.get(“访问令牌”);
stage.hide();
}捕获(格式错误){
e、 printStackTrace();
}
}
}
}
});
//创建场景
舞台剧集标题(“Skydrive”);
场景=新场景(边框窗格,750500);
舞台场景;
stage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}