Skip to content

Instantly share code, notes, and snippets.

@vlastachu
Created August 3, 2015 06:57
Show Gist options
  • Save vlastachu/1f857dfe251acaeb0a29 to your computer and use it in GitHub Desktop.
Save vlastachu/1f857dfe251acaeb0a29 to your computer and use it in GitHub Desktop.
package com.company;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
static String getServerTime() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.format(calendar.getTime());
}
static final Pattern codePattern = Pattern.compile("GET /\\?code=(.*) HTTP/1\\.1");
public static void main(String[] args) throws Exception {
int port = 9004;
ServerSocket serverSocket = new ServerSocket(port);
//System.err.println("Serveur lanc? sur le port : " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println(">>>> new client");
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String s, code = "";
while ((s = in.readLine()) != null) {
System.out.println(s);
if(s.startsWith("GET")){
Matcher m =codePattern.matcher(s);
if(m.find()) {
code = m.group(1);
System.out.println(">>>> code = " + code);
}
}
if (s.isEmpty()) {
break;
}
}
out.write("<p>OK</p>");
// on ferme les flux.
System.out.println(">>>> END");
out.close();
in.close();
clientSocket.close();
if(!code.isEmpty())
sendReq(code);
}
}
static void sendReq(String code) throws Exception{
String response = "code=" + code
+ "&client_id=337164869739-qfql761p2d9mch06fu5jle06hna92ubg.apps.googleusercontent.com"
+ "&client_secret=FC6TdOB05RjyHzyUOJ8u-HsG"
+ "&redirect_uri=http://localhost:9004"
+ "&grant_type=authorization_code";
URL url = new URL("https://www.googleapis.com/oauth2/v3/token");
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.addRequestProperty("Content-type", "application/x-www-form-urlencoded");
con.addRequestProperty("Host", "www.googleapis.com");
con.setRequestMethod("POST");
con.setFixedLengthStreamingMode(response.length());
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(response);
wr.flush();
wr.close();
// Socket s = new Socket(InetAddress.getByName("accounts.google.com"), 80);
// PrintWriter pw = new PrintWriter(s.getOutputStream());
// pw.write("POST /oauth2/v3/token HTTP/1.1\r\n");
// pw.write("Host: www.googleapis.com \r\n");
// pw.write("Content-Type: application/x-www-form-urlencoded\r\n");
// pw.println("Content-length: " + response.length());
// pw.write("\r\n");
// pw.write(response);
// pw.flush();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String t;
System.out.println(">>>> Begin write post pesonse");
while((t = br.readLine()) != null) System.out.println(t);
br.close();
con.disconnect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment