Last active
December 14, 2015 00:09
-
-
Save jdennes/4997342 to your computer and use it in GitHub Desktop.
A Java application to demonstrate authenticating with the Campaign Monitor API using OAuth. Uses the createsend-java library as well as the Spark micro web framework (but could easily be adapted for use with any Java web application framework).
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
// Ensure you set the CREATESEND_CLIENT_ID, CREATESEND_CLIENT_SECRET, | |
// and CREATESEND_REDIRECT_URI environment variables for your registered | |
// OAuth application. | |
import static spark.Spark.*; | |
import spark.*; | |
import com.createsend.General; | |
import com.createsend.models.clients.ClientBasics; | |
import com.createsend.models.OAuthTokenDetails; | |
import com.createsend.util.AuthenticationDetails; | |
import com.createsend.util.OAuthAuthenticationDetails; | |
import com.createsend.util.exceptions.CreateSendException; | |
public class CreateSendOAuthTest { | |
public static void main(String[] args) { | |
setPort(Integer.parseInt(System.getenv("PORT"))); | |
get(new Route("/") { | |
@Override | |
public Object handle(Request req, Response res) { | |
String authorizeUrl = General.getAuthorizeUrl( | |
Integer.parseInt(System.getenv("CREATESEND_CLIENT_ID")), | |
System.getenv("CREATESEND_REDIRECT_URI"), | |
"ViewReports,CreateCampaigns,SendCampaigns", | |
null); | |
res.redirect(authorizeUrl); | |
return "Redirected."; | |
} | |
}); | |
get(new Route("/exchange_token") { | |
@Override | |
public Object handle(Request req, Response res) { | |
String code = req.queryParams("code"); | |
String response = ""; | |
try { | |
OAuthTokenDetails tokenDetails = General.exchangeToken( | |
Integer.parseInt(System.getenv("CREATESEND_CLIENT_ID")), | |
System.getenv("CREATESEND_CLIENT_SECRET"), | |
System.getenv("CREATESEND_REDIRECT_URI"), | |
code); | |
String accessToken = tokenDetails.access_token; | |
String refreshToken = tokenDetails.refresh_token; | |
response = "<pre>"; | |
response += "Your user is successfully authenticated. Here are the details you need:<br/><br/>"; | |
response += "access token: " + accessToken + "<br/>"; | |
response += "refresh token: " + refreshToken + "<br/>"; | |
response += "<br/><br/>"; | |
AuthenticationDetails auth = new OAuthAuthenticationDetails( | |
accessToken, refreshToken); | |
General general = new General(auth); | |
ClientBasics[] clients = general.getClients(); | |
response += "We've made an API call too. Here are your clients:<br/><br/>"; | |
for (ClientBasics c : clients) { | |
response += c.Name + "<br/>"; | |
} | |
response += "</pre>"; | |
} catch (CreateSendException ex) { | |
response = "Errors!<br/><br/>"; | |
response += ex.toString(); | |
} | |
return response; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment