In this example we are going to see about how to use Flickr OAuth 1.0a using Scribe Java
Flickr OAuth 1.0a Example using Scribe
FlickrExample.java
01.
import
java.util.Scanner;
02.
import
com.github.scribejava.core.builder.ServiceBuilder;
03.
import
com.github.scribejava.apis.FlickrApi;
04.
import
com.github.scribejava.core.model.OAuth1AccessToken;
05.
import
com.github.scribejava.core.model.OAuth1RequestToken;
06.
import
com.github.scribejava.core.model.OAuthRequest;
07.
import
com.github.scribejava.core.model.Response;
08.
import
com.github.scribejava.core.model.Verb;
09.
import
com.github.scribejava.core.oauth.OAuth10aService;
10.
import
java.io.IOException;
11.
12.
public
abstract
class
FlickrExample {
13.
14.
private
static
final
String PROTECTED_RESOURCE_URL =
"http://ift.tt/19PqDP5"
;
15.
16.
public
static
void
main(String... args)
throws
IOException {
17.
// Replace these with your own api key and secret
18.
final
String apiKey =
"your_app_id"
;
19.
final
String apiSecret =
"your_api_secret"
;
20.
final
OAuth10aService service =
new
ServiceBuilder()
21.
.apiKey(apiKey)
22.
.apiSecret(apiSecret)
23.
.build(FlickrApi.instance());
24.
final
Scanner in =
new
Scanner(System.in);
25.
26.
System.out.println(
"=== Flickr's OAuth Workflow ==="
);
27.
System.out.println();
28.
29.
// Obtain the Request Token
30.
System.out.println(
"Fetching the Request Token..."
);
31.
final
OAuth1RequestToken requestToken = service.getRequestToken();
32.
System.out.println(
"Got the Request Token!"
);
33.
System.out.println();
34.
35.
System.out.println(
"Now go and authorize ScribeJava here:"
);
36.
final
String authorizationUrl = service.getAuthorizationUrl(requestToken);
37.
System.out.println(authorizationUrl +
"&perms=read"
);
38.
System.out.println(
"And paste the verifier here"
);
39.
System.out.print(
">>"
);
40.
final
String oauthVerifier = 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.
final
OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
46.
System.out.println(
"Got the Access Token!"
);
47.
System.out.println(
"(if your curious it looks like this: "
+ accessToken
48.
+
", 'rawResponse'='"
+ accessToken.getRawResponse() +
"')"
);
49.
System.out.println();
50.
51.
// Now let's go and ask for a protected resource!
52.
System.out.println(
"Now we're going to access a protected resource..."
);
53.
final
OAuthRequest request =
new
OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL, service);
54.
request.addQuerystringParameter(
"method"
,
"flickr.test.login"
);
55.
service.signRequest(accessToken, request);
56.
final
Response response = request.send();
57.
System.out.println(
"Got it! Lets see what we found..."
);
58.
System.out.println();
59.
System.out.println(response.getBody());
60.
61.
System.out.println();
62.
System.out.println(
"Thats it man! Go and build something awesome with ScribeJava! :)"
);
63.
}
64.
}
0 comments:
Post a Comment