Last active
October 18, 2016 01:02
-
-
Save acmi/6d0e3ec341537713074b to your computer and use it in GitHub Desktop.
VK auth javafx app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application | |
import javafx.application.Platform | |
import javafx.beans.InvalidationListener | |
import javafx.beans.Observable | |
import javafx.scene.Scene | |
import javafx.scene.web.WebView | |
import javafx.stage.Stage | |
class VKAuth extends Application{ | |
private static final String REDIRECT_URI = 'https://oauth.vk.com/blank.html' | |
@Override | |
void start(Stage primaryStage) throws Exception { | |
if (!parameters.named.containsKey('client_id')){ | |
System.err.println 'client_id not specified' | |
Platform.exit() | |
} | |
def url = new StringBuilder('https://oauth.vk.com/authorize') | |
url << "?client_id=${parameters.named['client_id']}" | |
url << "&redirect_uri=$REDIRECT_URI" | |
if (parameters.named.containsKey('scope')) | |
url << "&scope=${parameters.named['scope']}" | |
if (parameters.named.containsKey('v')) | |
url << "&v=${parameters.named['v']}" | |
url << "&display=mobile" | |
url << "&response_type=token" | |
def webView = new WebView() | |
webView.engine.locationProperty().addListener({Observable observable-> | |
def loc = webView.engine.location | |
if (loc.startsWith(REDIRECT_URI)){ | |
def result = loc.substring(REDIRECT_URI.size()+1).split('&').collectEntries { | |
def t = it.split('=') | |
[(t[0]):t[1]] | |
} | |
if (result.containsKey('access_token')){ | |
println result['access_token'] | |
}else if (result.containsKey('error')){ | |
println "${result['error']}: ${result['error_description']}" | |
}else { | |
println result | |
} | |
Platform.exit() | |
} | |
} as InvalidationListener) | |
webView.engine.load(url.toString()) | |
primaryStage.scene = new Scene(webView, 360, 480) | |
primaryStage.show() | |
} | |
static void main(String[] args){ | |
launch(VKAuth, args) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment