Skip to content

Commit e000757

Browse files
author
boncey
committed
Added FlickrExample
Example for Flickr OAuth
1 parent 41777bc commit e000757

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)