|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import com.github.scribejava.apis.FlickrApi; |
| 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 FlickrExampleWithPerms { |
| 17 | + |
| 18 | + private static final String PROTECTED_RESOURCE_URL = "http://api.flickr.com/services/rest/"; |
| 19 | + |
| 20 | + private FlickrExampleWithPerms() { |
| 21 | + } |
| 22 | + |
| 23 | + public static void main(String... args) throws IOException, InterruptedException, ExecutionException { |
| 24 | + // Replace these with your own api key and secret |
| 25 | + final String apiKey = "your_app_id"; |
| 26 | + final String apiSecret = "your_api_secret"; |
| 27 | + |
| 28 | + final OAuth10aService service = new ServiceBuilder() |
| 29 | + .apiKey(apiKey) |
| 30 | + .apiSecret(apiSecret) |
| 31 | + .build(new FlickrApi(FlickrApi.FLICKR_PERM.DELETE)); |
| 32 | + final Scanner in = new Scanner(System.in); |
| 33 | + |
| 34 | + System.out.println("=== Flickr's OAuth Workflow ==="); |
| 35 | + System.out.println(); |
| 36 | + |
| 37 | + // Obtain the Request Token |
| 38 | + System.out.println("Fetching the Request Token..."); |
| 39 | + final OAuth1RequestToken requestToken = service.getRequestToken(); |
| 40 | + System.out.println("Got the Request Token!"); |
| 41 | + System.out.println(); |
| 42 | + |
| 43 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 44 | + final String authorizationUrl = service.getAuthorizationUrl(requestToken); |
| 45 | + System.out.println(authorizationUrl); |
| 46 | + System.out.println("And paste the verifier here"); |
| 47 | + System.out.print(">>"); |
| 48 | + final String oauthVerifier = in.nextLine(); |
| 49 | + System.out.println(); |
| 50 | + |
| 51 | + // Trade the Request Token and Verfier for the Access Token |
| 52 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 53 | + final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier); |
| 54 | + System.out.println("Got the Access Token!"); |
| 55 | + System.out.println("(if your curious it looks like this: " + accessToken |
| 56 | + + ", 'rawResponse'='" + accessToken.getRawResponse() + "')"); |
| 57 | + System.out.println(); |
| 58 | + |
| 59 | + // Now let's go and ask for a protected resource! |
| 60 | + System.out.println("Now we're going to access a protected resource..."); |
| 61 | + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 62 | + request.addQuerystringParameter("method", "flickr.test.login"); |
| 63 | + service.signRequest(accessToken, request); |
| 64 | + final Response response = service.execute(request); |
| 65 | + System.out.println("Got it! Lets see what we found..."); |
| 66 | + System.out.println(); |
| 67 | + System.out.println(response.getBody()); |
| 68 | + |
| 69 | + System.out.println(); |
| 70 | + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 71 | + } |
| 72 | +} |
0 commit comments