diff --git a/changelog.txt b/changelog.txt index 617567805..7c6bfde18 100644 --- a/changelog.txt +++ b/changelog.txt @@ -95,3 +95,4 @@ [1.3.3] * FEATURE: accessToken and requestToken timeouts default to 2 seconds and can be specified. + * FEATURE: Trello.com Api diff --git a/src/main/java/org/scribe/builder/api/TrelloApi.java b/src/main/java/org/scribe/builder/api/TrelloApi.java new file mode 100644 index 000000000..c0b21e3d5 --- /dev/null +++ b/src/main/java/org/scribe/builder/api/TrelloApi.java @@ -0,0 +1,35 @@ +package org.scribe.builder.api; + +import org.scribe.model.Token; + +/** + * OAuth access to the trello.com api. + *

+ * For more information visit http://trello.com/api + * + * @author Harald Wartig + */ +public class TrelloApi extends DefaultApi10a +{ + + private static final String AUTHORIZE_URL = "https://trello.com/1/OAuthAuthorizeToken?oauth_token=%s"; + + @Override + public String getRequestTokenEndpoint() + { + return "https://trello.com/1/OAuthGetRequestToken"; + } + + @Override + public String getAccessTokenEndpoint() + { + return "https://trello.com/1/OAuthGetAccessToken"; + } + + @Override + public String getAuthorizationUrl(Token requestToken) + { + return String.format(AUTHORIZE_URL, requestToken.getToken()); + } +} + diff --git a/src/test/java/org/scribe/examples/TrelloExample.java b/src/test/java/org/scribe/examples/TrelloExample.java new file mode 100644 index 000000000..d18c145e6 --- /dev/null +++ b/src/test/java/org/scribe/examples/TrelloExample.java @@ -0,0 +1,81 @@ +package org.scribe.examples; + +import org.scribe.builder.ServiceBuilder; +import org.scribe.builder.api.TrelloApi; +import org.scribe.model.*; +import org.scribe.oauth.OAuthService; + +import java.util.Scanner; + +/** + * Shows you how to use scribe with trello.com . + * + * @author Harald Wartig + */ +public class TrelloExample +{ + private static final String PROTECTED_RESOURCE_URL = "https://api.trello.com/1/members/me/boards"; + + public static void main(String[] args) + { + // Replace these with your own api key and secret + String apiKey = ""; + String apiSecret = ""; + + Scanner in = new Scanner(System.in); + + if (apiKey.isEmpty() || apiSecret.isEmpty()) { + System.out.println("=== Setup ==="); + System.out.println(); + + System.out.println("We need your apiKey:"); + System.out.print(">>"); + apiKey = in.nextLine(); + + System.out.println(".. and your apiSecret too:"); + System.out.print(">>"); + apiSecret = in.nextLine(); + } + + OAuthService service = new ServiceBuilder() + .provider(TrelloApi.class) + .apiKey(apiKey) + .apiSecret(apiSecret) + .build(); + + System.out.println("=== Trello's OAuth Workflow ==="); + System.out.println(); + + // Obtain the Request Token + System.out.println("Fetching the Request Token..."); + Token requestToken = service.getRequestToken(); + System.out.println("Got the Request Token!"); + System.out.println(); + + System.out.println("Now go and authorize Scribe here:"); + System.out.println(service.getAuthorizationUrl(requestToken)); + System.out.println("And paste the verifier here"); + System.out.print(">>"); + Verifier verifier = new Verifier(in.nextLine()); + System.out.println(); + + // Trade the Request Token and Verifier for the Access Token + System.out.println("Trading the Request Token for an Access Token..."); + Token accessToken = service.getAccessToken(requestToken, verifier); + System.out.println("Got the Access Token!"); + System.out.println("(if your curious it looks like this: " + accessToken + " )"); + System.out.println(); + + // Now let's go and ask for a protected resource! + System.out.println("Now we're going to access a protected resource..."); + OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); + service.signRequest(accessToken, request); + Response response = request.send(); + System.out.println("Got it! Lets take a look at your board list..."); + System.out.println(); + System.out.println(response.getBody()); + + System.out.println(); + System.out.println("Go and build something awesome with Scribe & trello.com :)"); + } +}