|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import com.github.scribejava.apis.EtsyApi; |
| 4 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 5 | +import com.github.scribejava.core.model.OAuth1AccessToken; |
| 6 | +import com.github.scribejava.core.model.OAuth1RequestToken; |
| 7 | +import com.github.scribejava.core.model.OAuthRequest; |
| 8 | +import com.github.scribejava.core.model.Response; |
| 9 | +import com.github.scribejava.core.model.Verb; |
| 10 | +import com.github.scribejava.core.oauth.OAuth10aService; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.Scanner; |
| 14 | +import java.util.concurrent.ExecutionException; |
| 15 | + |
| 16 | +public final class EtsyExample { |
| 17 | + |
| 18 | + private static final String PROTECTED_RESOURCE_URL = "https://openapi.etsy.com/v2/users/__SELF__"; |
| 19 | + |
| 20 | + private EtsyExample() { |
| 21 | + } |
| 22 | + |
| 23 | + public static void main(String[] args) throws InterruptedException, ExecutionException, IOException { |
| 24 | + // Replace with your api and secret key |
| 25 | + final OAuth10aService service = new ServiceBuilder("your api key") |
| 26 | + .apiSecret("your secret key") |
| 27 | + .build(EtsyApi.instance()); |
| 28 | + final Scanner in = new Scanner(System.in); |
| 29 | + |
| 30 | + System.out.println("=== Etsy's OAuth Workflow ==="); |
| 31 | + System.out.println(); |
| 32 | + |
| 33 | + // Obtain the Request Token |
| 34 | + System.out.println("Fetching the Request Token..."); |
| 35 | + final OAuth1RequestToken requestToken = service.getRequestToken(); |
| 36 | + System.out.println("Got the Request Token!"); |
| 37 | + System.out.println(); |
| 38 | + |
| 39 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 40 | + System.out.println(service.getAuthorizationUrl(requestToken)); |
| 41 | + System.out.println("And paste the verifier here"); |
| 42 | + System.out.print(">>"); |
| 43 | + final String oauthVerifier = in.nextLine(); |
| 44 | + System.out.println(); |
| 45 | + |
| 46 | + // Trade the Request Token and Verifier for the Access Token |
| 47 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 48 | + final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier); |
| 49 | + System.out.println("Got the Access Token!"); |
| 50 | + System.out.println("(if your curious it looks like this: " + accessToken |
| 51 | + + ", 'rawResponse'='" + accessToken.getRawResponse() + "')"); |
| 52 | + System.out.println(); |
| 53 | + |
| 54 | + // Now let's go and ask for a protected resource! |
| 55 | + System.out.println("Now we're going to access a protected resource..."); |
| 56 | + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 57 | + service.signRequest(accessToken, request); |
| 58 | + final Response response = service.execute(request); |
| 59 | + System.out.println("Got it! Lets see what we found..."); |
| 60 | + System.out.println(); |
| 61 | + System.out.println(response.getBody()); |
| 62 | + |
| 63 | + System.out.println(); |
| 64 | + System.out.println("That's it man! Go and build something awesome with ScribeJava! :)"); |
| 65 | + } |
| 66 | +} |
0 commit comments