Skip to content

Commit 47bcf7b

Browse files
committed
Merge pull request scribejava#591 from martinmalek/master
Adding PinterestApi and PinterestExample
2 parents 4d927ea + cb6bb1a commit 47bcf7b

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
import com.github.scribejava.core.extractors.AccessTokenExtractor;
5+
import com.github.scribejava.core.extractors.JsonTokenExtractor;
6+
import com.github.scribejava.core.model.OAuthConfig;
7+
import com.github.scribejava.core.model.OAuthConstants;
8+
import com.github.scribejava.core.model.Verb;
9+
import com.github.scribejava.core.utils.OAuthEncoder;
10+
import com.github.scribejava.core.utils.Preconditions;
11+
12+
public class PinterestApi extends DefaultApi20 {
13+
14+
private static final String AUTHORIZE_URL = "https://api.pinterest.com/oauth?response_type=code&client_id=%s&redirect_uri=%s";
15+
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
16+
17+
@Override
18+
public String getAccessTokenEndpoint() {
19+
return "https://api.pinterest.com/v1/oauth/token?grant_type=" + OAuthConstants.AUTHORIZATION_CODE;
20+
}
21+
22+
@Override
23+
public Verb getAccessTokenVerb() {
24+
return Verb.POST;
25+
}
26+
27+
@Override
28+
public String getAuthorizationUrl(OAuthConfig config) {
29+
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Pinterest does not support OOB");
30+
31+
// Append scope if present
32+
if (config.hasScope()) {
33+
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.
34+
getScope()));
35+
} else {
36+
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
37+
}
38+
}
39+
40+
@Override
41+
public AccessTokenExtractor getAccessTokenExtractor() {
42+
return new JsonTokenExtractor();
43+
}
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.apis.PinterestApi;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.core.model.OAuthRequest;
6+
import com.github.scribejava.core.model.Response;
7+
import com.github.scribejava.core.model.Token;
8+
import com.github.scribejava.core.model.Verb;
9+
import com.github.scribejava.core.model.Verifier;
10+
import com.github.scribejava.core.oauth.OAuthService;
11+
12+
import java.util.Scanner;
13+
14+
public class PinterestExample {
15+
16+
private static final String PROTECTED_RESOURCE_URL = "https://api.pinterest.com/v1/me/?access_token?access_token=";
17+
private static final Token EMPTY_TOKEN = null;
18+
19+
public static void main(String[] args) {
20+
// Replace these with your own api key and secret
21+
String apiKey = "your_app_id";
22+
String apiSecret = "your_app_secret";
23+
OAuthService service = new ServiceBuilder()
24+
.provider(PinterestApi.class)
25+
.apiKey(apiKey)
26+
.apiSecret(apiSecret)
27+
.scope("read_public,write_public,read_relationships,write_relationships")
28+
.callback("https://localhost:9000/") // Add as valid callback in developer portal
29+
.build();
30+
Scanner in = new Scanner(System.in);
31+
32+
System.out.println("=== Pinterest's OAuth Workflow ===");
33+
System.out.println();
34+
35+
// Obtain the Authorization URL
36+
System.out.println("Fetching the Authorization URL...");
37+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
38+
System.out.println("Got the Authorization URL!");
39+
System.out.println("Now go and authorize ScribeJava here:");
40+
System.out.println(authorizationUrl);
41+
System.out.println("And paste the authorization code 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(EMPTY_TOKEN, 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 + accessToken.getToken(), service);
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.getCode());
61+
System.out.println(response.getBody());
62+
63+
System.out.println();
64+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
65+
66+
}
67+
}

0 commit comments

Comments
 (0)