Skip to content

Commit 40d2ba9

Browse files
author
Martin Malek
committed
Adding PinterestApi and PinterestExample
1 parent 4d927ea commit 40d2ba9

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.*;
6+
import com.github.scribejava.core.oauth.OAuthService;
7+
8+
import java.util.Scanner;
9+
10+
public class PinterestExample {
11+
12+
private static final String PROTECTED_RESOURCE_URL = "https://api.pinterest.com/v1/me/?access_token?access_token=";
13+
private static final Token EMPTY_TOKEN = null;
14+
15+
public static void main(String[] args) {
16+
// Replace these with your own api key and secret
17+
String apiKey = "your_app_id";
18+
String apiSecret = "your_app_secret";
19+
OAuthService service = new ServiceBuilder()
20+
.provider(PinterestApi.class)
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();
26+
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+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
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+
Verifier verifier = new Verifier(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+
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
45+
System.out.println("Got the Access Token!");
46+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
47+
System.out.println();
48+
49+
// Now let's go and ask for a protected resource!
50+
System.out.println("Now we're going to access a protected resource...");
51+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL + accessToken.getToken(), service);
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.getCode());
57+
System.out.println(response.getBody());
58+
59+
System.out.println();
60+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
61+
62+
}
63+
}

0 commit comments

Comments
 (0)