Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit47bcf7b

Browse files
committed
Merge pull requestscribejava#591 from martinmalek/master
Adding PinterestApi and PinterestExample
2 parents4d927ea +cb6bb1a commit47bcf7b

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+
packagecom.github.scribejava.apis;
2+
3+
importcom.github.scribejava.core.builder.api.DefaultApi20;
4+
importcom.github.scribejava.core.extractors.AccessTokenExtractor;
5+
importcom.github.scribejava.core.extractors.JsonTokenExtractor;
6+
importcom.github.scribejava.core.model.OAuthConfig;
7+
importcom.github.scribejava.core.model.OAuthConstants;
8+
importcom.github.scribejava.core.model.Verb;
9+
importcom.github.scribejava.core.utils.OAuthEncoder;
10+
importcom.github.scribejava.core.utils.Preconditions;
11+
12+
publicclassPinterestApiextendsDefaultApi20 {
13+
14+
privatestaticfinalStringAUTHORIZE_URL ="https://api.pinterest.com/oauth?response_type=code&client_id=%s&redirect_uri=%s";
15+
privatestaticfinalStringSCOPED_AUTHORIZE_URL =AUTHORIZE_URL +"&scope=%s";
16+
17+
@Override
18+
publicStringgetAccessTokenEndpoint() {
19+
return"https://api.pinterest.com/v1/oauth/token?grant_type=" +OAuthConstants.AUTHORIZATION_CODE;
20+
}
21+
22+
@Override
23+
publicVerbgetAccessTokenVerb() {
24+
returnVerb.POST;
25+
}
26+
27+
@Override
28+
publicStringgetAuthorizationUrl(OAuthConfigconfig) {
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+
returnString.format(SCOPED_AUTHORIZE_URL,config.getApiKey(),OAuthEncoder.encode(config.getCallback()),OAuthEncoder.encode(config.
34+
getScope()));
35+
}else {
36+
returnString.format(AUTHORIZE_URL,config.getApiKey(),OAuthEncoder.encode(config.getCallback()));
37+
}
38+
}
39+
40+
@Override
41+
publicAccessTokenExtractorgetAccessTokenExtractor() {
42+
returnnewJsonTokenExtractor();
43+
}
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
packagecom.github.scribejava.apis.examples;
2+
3+
importcom.github.scribejava.apis.PinterestApi;
4+
importcom.github.scribejava.core.builder.ServiceBuilder;
5+
importcom.github.scribejava.core.model.OAuthRequest;
6+
importcom.github.scribejava.core.model.Response;
7+
importcom.github.scribejava.core.model.Token;
8+
importcom.github.scribejava.core.model.Verb;
9+
importcom.github.scribejava.core.model.Verifier;
10+
importcom.github.scribejava.core.oauth.OAuthService;
11+
12+
importjava.util.Scanner;
13+
14+
publicclassPinterestExample {
15+
16+
privatestaticfinalStringPROTECTED_RESOURCE_URL ="https://api.pinterest.com/v1/me/?access_token?access_token=";
17+
privatestaticfinalTokenEMPTY_TOKEN =null;
18+
19+
publicstaticvoidmain(String[]args) {
20+
// Replace these with your own api key and secret
21+
StringapiKey ="your_app_id";
22+
StringapiSecret ="your_app_secret";
23+
OAuthServiceservice =newServiceBuilder()
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+
Scannerin =newScanner(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+
StringauthorizationUrl =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+
Verifierverifier =newVerifier(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+
TokenaccessToken =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+
OAuthRequestrequest =newOAuthRequest(Verb.GET,PROTECTED_RESOURCE_URL +accessToken.getToken(),service);
56+
service.signRequest(accessToken,request);
57+
Responseresponse =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)

[8]ページ先頭

©2009-2025 Movatter.jp