|
| 1 | +package org.scribe.examples; |
| 2 | + |
| 3 | +import org.scribe.builder.ServiceBuilder; |
| 4 | +import org.scribe.builder.api.ImgUrApi; |
| 5 | +import org.scribe.model.OAuthRequest; |
| 6 | +import org.scribe.model.Response; |
| 7 | +import org.scribe.model.Token; |
| 8 | +import org.scribe.model.Verb; |
| 9 | +import org.scribe.model.Verifier; |
| 10 | +import org.scribe.oauth.OAuthService; |
| 11 | + |
| 12 | +import java.util.Scanner; |
| 13 | + |
| 14 | +public class ImgUrExample |
| 15 | +{ |
| 16 | + private static final String PROTECTED_RESOURCE_URL = "http://api.imgur.com/2/account.json"; |
| 17 | + |
| 18 | + public static void main(String[] args) |
| 19 | + { |
| 20 | + // Replace these with your own api key and secret (you'll need an read/write api key) |
| 21 | + String apiKey = "your_app_id"; |
| 22 | + String apiSecret = "your_api_secret"; |
| 23 | + OAuthService service = new ServiceBuilder().provider(ImgUrApi.class).apiKey(apiKey).apiSecret(apiSecret).build(); |
| 24 | + Scanner in = new Scanner(System.in); |
| 25 | + |
| 26 | + System.out.println("=== ImgUr's OAuth Workflow ==="); |
| 27 | + System.out.println(); |
| 28 | + |
| 29 | + // Obtain the Request Token |
| 30 | + System.out.println("Fetching the Request Token..."); |
| 31 | + Token requestToken = service.getRequestToken(); |
| 32 | + System.out.println("Got the Request Token!"); |
| 33 | + System.out.println(); |
| 34 | + |
| 35 | + System.out.println("Now go and authorize Scribe here:"); |
| 36 | + String authorizationUrl = service.getAuthorizationUrl(requestToken); |
| 37 | + System.out.println(authorizationUrl); |
| 38 | + System.out.println("And paste the verifier here"); |
| 39 | + System.out.print(">>"); |
| 40 | + Verifier verifier = new Verifier(in.nextLine()); |
| 41 | + System.out.println(); |
| 42 | + |
| 43 | + // Trade the Request Token and Verfier for the Access Token |
| 44 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 45 | + Token accessToken = service.getAccessToken(requestToken, verifier); |
| 46 | + System.out.println("Got the Access Token!"); |
| 47 | + System.out.println("(if your curious it looks like this: " + accessToken + " )"); |
| 48 | + System.out.println(); |
| 49 | + |
| 50 | + // Now let's go and ask for a protected resource! |
| 51 | + System.out.println("Now we're going to access a protected resource..."); |
| 52 | + OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 53 | + service.signRequest(accessToken, request); |
| 54 | + Response response = request.send(); |
| 55 | + System.out.println("Got it! Lets see what we found..."); |
| 56 | + System.out.println(); |
| 57 | + System.out.println(response.getBody()); |
| 58 | + |
| 59 | + System.out.println(); |
| 60 | + System.out.println("Thats it man! Go and build something awesome with Scribe! :)"); |
| 61 | + } |
| 62 | +} |
0 commit comments