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