In this example we are going to see about how to use Pinterest OAuth 2.0 using Scribe Java
Pinterest OAuth 2.0 Example with Scope
PinterestExample.java
01.
import
com.github.scribejava.apis.PinterestApi;
02.
import
com.github.scribejava.core.builder.ServiceBuilder;
03.
import
com.github.scribejava.core.model.OAuth2AccessToken;
04.
import
com.github.scribejava.core.model.OAuthRequest;
05.
import
com.github.scribejava.core.model.Response;
06.
import
com.github.scribejava.core.model.Verb;
07.
import
com.github.scribejava.core.oauth.OAuth20Service;
08.
import
java.io.IOException;
09.
10.
import
java.util.Scanner;
11.
12.
public
abstract
class
PinterestExample {
13.
14.
private
static
final
String PROTECTED_RESOURCE_URL =
"http://ift.tt/29ycqdl"
;
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_app_secret"
;
20.
final
OAuth20Service service =
new
ServiceBuilder()
21.
.apiKey(apiKey)
22.
.apiSecret(apiSecret)
23.
.scope(
"read_public,write_public,read_relationships,write_relationships"
)
24.
.callback(
"https://localhost:9000/"
) // Add as valid callback in developer portal
25.
.build(PinterestApi.instance());
26.
final
Scanner in =
new
Scanner(System.in);
27.
28.
System.out.println(
"=== Pinterest's OAuth Workflow ==="
);
29.
System.out.println();
30.
31.
// Obtain the Authorization URL
32.
System.out.println(
"Fetching the Authorization URL..."
);
33.
final
String authorizationUrl = service.getAuthorizationUrl();
34.
System.out.println(
"Got the Authorization URL!"
);
35.
System.out.println(
"Now go and authorize ScribeJava here:"
);
36.
System.out.println(authorizationUrl);
37.
System.out.println(
"And paste the authorization code here"
);
38.
System.out.print(
">>"
);
39.
final
String code = in.nextLine();
40.
System.out.println();
41.
42.
// Trade the Request Token and Verfier for the Access Token
43.
System.out.println(
"Trading the Request Token for an Access Token..."
);
44.
final
OAuth2AccessToken accessToken = service.getAccessToken(code);
45.
System.out.println(
"Got the Access Token!"
);
46.
System.out.println(
"(if your curious it looks like this: "
+ accessToken
47.
+
", 'rawResponse'='"
+ accessToken.getRawResponse() +
"')"
);
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.
final
OAuthRequest request =
new
OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL + accessToken.getAccessToken(),
53.
service);
54.
service.signRequest(accessToken, request);
55.
final
Response response = request.send();
56.
System.out.println(
"Got it! Lets see what we found..."
);
57.
System.out.println();
58.
System.out.println(response.getCode());
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.
}
65.
}
0 comments:
Post a Comment