1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| #!/usr/bin/env groovy
@Grab("org.mortbay.jetty:jetty-embedded:6.1.26")
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import javax.servlet.ServletException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.mortbay.jetty.Connector
import org.mortbay.jetty.Handler
import org.mortbay.jetty.Request
import org.mortbay.jetty.Server
import org.mortbay.jetty.handler.AbstractHandler
def appId = "XXXXXXXXXXXXXXX"
def localHost = "localhost"
def localPort = 9999
def urlText = "https://www.facebook.com/dialog/oauth?" + [
"client_id=${appId}",
"redirect_uri=" + URLEncoder.encode("http://${localHost}:${localPort}/", "utf-8"),
"scope=" + URLEncoder.encode("email,read_stream,friends_about_me,user_about_me", "utf-8"),
"response_type=token",
].join("&")
def browserProcess = browse(urlText)
server = new Server(localPort)
server.connectors.each {
it.host = "${localHost}"
}
server.handler = new Callback(browserProcess: browserProcess)
def serverThread = Thread.start {
server.start()
}
def browse(String url) throws ClassNotFoundException, IllegalAccessException,
IllegalArgumentException, InterruptedException, InvocationTargetException, IOException,
NoSuchMethodException {
String osName = System.getProperty("os.name", "")
if (osName.startsWith("Mac OS")) {
Class fileMgr = Class.forName("com.apple.eio.FileManager")
Method openURL = fileMgr.getDeclaredMethod("openURL", [String.class] as Class[])
openURL.invoke(null, [url] as Object[])
} else if (osName.startsWith("Windows")) {
return Runtime.runtime.exec("rundll32 url.dll,FileProtocolHandler " + url)
} else {
def browser = ["firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"].find {
Runtime.runtime.exec(["which", it] as String[]).waitFor() == 0
}
if (!browser)
throw new NoSuchMethodException("Could not find web browser")
return Runtime.runtime.exec(browser + " ${url}")
}
}
class Callback extends AbstractHandler {
def browserProcess
void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
throws IOException, ServletException {
println request.requestURL
request.handled = true
this.browserProcess.destroy()
}
}
|