Skip to content

Instantly share code, notes, and snippets.

@olivierbellone
Created November 22, 2015 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivierbellone/4e5cf13ccc98124b1551 to your computer and use it in GitHub Desktop.
Save olivierbellone/4e5cf13ccc98124b1551 to your computer and use it in GitHub Desktop.
Stripe OAuth Example -- Java 7 + Spark 1
<!doctype html>
<head>
<title>Stripe OAuth Example</title>
</head>
<body>
${token}
</body>
</html>
<!doctype html>
<head>
<title>Stripe OAuth Example</title>
</head>
<body>
<a href="/authorize">Connect with Stripe</a>
</body>
</html>
package com.stripe;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.net.URI;
import static spark.Spark.get;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.template.freemarker.FreeMarkerRoute;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class StripeSpark1OAuthApp {
public static final String CLIENT_ID = "YOUR_CLIENT_ID";
public static final String API_KEY = "YOUR_SECRET_API_KEY";
public static final String TOKEN_URI
= "https://connect.stripe.com/oauth/token";
public static final String AUTHORIZE_URI
= "https://connect.stripe.com/oauth/authorize";
public static void main(final String[] args) {
get(new FreeMarkerRoute("/") {
@Override
public ModelAndView handle(final Request request,
final Response response) {
Map<String, Object> viewObjects
= new HashMap<String, Object>();
return modelAndView(viewObjects, "index.ftl");
}
});
get(new Route("/authorize") {
@Override
public Object handle(final Request request,
final Response response) {
try {
URI uri = new URIBuilder(AUTHORIZE_URI)
.setParameter("response_type", "code")
.setParameter("scope", "read_write")
.setParameter("client_id", CLIENT_ID)
.build();
// Redirect to Stripe /oauth/authorize endpoint
response.status(201);
response.redirect(uri.toString());
} catch (Exception e) {
response.status(500);
}
return "";
}
});
get(new FreeMarkerRoute("/oauth/callback") {
@Override
public ModelAndView handle(final Request request,
final Response response) {
Map<String, Object> viewObjects
= new HashMap<String, Object>();
try {
CloseableHttpClient httpClient
= HttpClients.createDefault();
String code = request.queryParams("code");
URI uri = new URIBuilder(TOKEN_URI)
.setParameter("client_secret", API_KEY)
.setParameter("grant_type", "authorization_code")
.setParameter("client_id", CLIENT_ID)
.setParameter("code", code)
.build();
// Make /oauth/token endpoint POST request
HttpPost httpPost = new HttpPost(uri);
CloseableHttpResponse resp = httpClient.execute(httpPost);
// Grab access_token (use this as your user's API key)
String bodyAsString
= EntityUtils.toString(resp.getEntity());
Type t = new TypeToken<Map<String, String>>() { }.getType();
Map<String, String> map
= new GsonBuilder().create().fromJson(bodyAsString, t);
String token = map.get("access_token");
viewObjects.put("token", token);
} catch (Exception e) {
response.status(500);
}
return modelAndView(viewObjects, "callback.ftl");
}
});
}
}
@olivierbellone
Copy link
Author

You can find a complete Maven project for this code here.

If you use Java 1.8+, there is another version that makes use of Spark 2.x and lambda expressions here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment