Skip to content

Commit 6b51681

Browse files
committed
Added EtsyAPI
1 parent 48d17c2 commit 6b51681

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ScribeJava support out-of-box several HTTP clients:
3939
* Box (https://www.box.com/)
4040
* Digg (http://digg.com/)
4141
* Доктор на работе (https://www.doktornarabote.ru/)
42+
* Etsy (https://www.etsy.com/)
4243
* Facebook (https://www.facebook.com/)
4344
* Flickr (https://www.flickr.com/)
4445
* Foursquare (https://foursquare.com/)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi10a;
4+
import com.github.scribejava.core.model.OAuth1RequestToken;
5+
6+
public class EtsyApi extends DefaultApi10a {
7+
8+
private static final String AUTHORIZE_URL = "https://www.etsy.com/oauth/signin?oauth_token=%s";
9+
private static final String ACCESS_TOKEN_URL = "https://openapi.etsy.com/v2/oauth/access_token";
10+
private static final String REQUEST_TOKEN_URL = "https://openapi.etsy.com/v2/oauth/request_token";
11+
12+
private final String scopeAsString;
13+
14+
private EtsyApi() {
15+
scopeAsString = null;
16+
}
17+
18+
private EtsyApi(String... scopes) {
19+
final StringBuilder builder = new StringBuilder();
20+
for (String scope : scopes) {
21+
builder.append("%20").append(scope);
22+
}
23+
scopeAsString = "?scope=" + builder.substring(3);
24+
}
25+
26+
private static class InstanceHolder {
27+
private static final EtsyApi INSTANCE = new EtsyApi();
28+
}
29+
30+
public static EtsyApi instance() {
31+
return InstanceHolder.INSTANCE;
32+
}
33+
34+
public static EtsyApi instance(String... scopes) {
35+
return scopes == null || scopes.length == 0 ? instance() : new EtsyApi(scopes);
36+
}
37+
38+
@Override
39+
public String getAccessTokenEndpoint() {
40+
return ACCESS_TOKEN_URL;
41+
}
42+
43+
@Override
44+
public String getRequestTokenEndpoint() {
45+
return scopeAsString == null ? REQUEST_TOKEN_URL : REQUEST_TOKEN_URL + scopeAsString;
46+
}
47+
48+
@Override
49+
public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
50+
return String.format(AUTHORIZE_URL, requestToken.getToken());
51+
}
52+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.apis.EtsyApi;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.core.model.OAuth1AccessToken;
6+
import com.github.scribejava.core.model.OAuth1RequestToken;
7+
import com.github.scribejava.core.model.OAuthRequest;
8+
import com.github.scribejava.core.model.Response;
9+
import com.github.scribejava.core.model.Verb;
10+
import com.github.scribejava.core.oauth.OAuth10aService;
11+
12+
import java.io.IOException;
13+
import java.util.Scanner;
14+
import java.util.concurrent.ExecutionException;
15+
16+
public final class EtsyExample {
17+
18+
private static final String PROTECTED_RESOURCE_URL = "https://openapi.etsy.com/v2/users/__SELF__";
19+
20+
private EtsyExample() {
21+
}
22+
23+
public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {
24+
// Replace with your api and secret key
25+
final OAuth10aService service = new ServiceBuilder("your api key")
26+
.apiSecret("your secret key")
27+
.build(EtsyApi.instance());
28+
final Scanner in = new Scanner(System.in);
29+
30+
System.out.println("=== Etsy's OAuth Workflow ===");
31+
System.out.println();
32+
33+
// Obtain the Request Token
34+
System.out.println("Fetching the Request Token...");
35+
final OAuth1RequestToken requestToken = service.getRequestToken();
36+
System.out.println("Got the Request Token!");
37+
System.out.println();
38+
39+
System.out.println("Now go and authorize ScribeJava here:");
40+
System.out.println(service.getAuthorizationUrl(requestToken));
41+
System.out.println("And paste the verifier here");
42+
System.out.print(">>");
43+
final String oauthVerifier = in.nextLine();
44+
System.out.println();
45+
46+
// Trade the Request Token and Verifier for the Access Token
47+
System.out.println("Trading the Request Token for an Access Token...");
48+
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
49+
System.out.println("Got the Access Token!");
50+
System.out.println("(if your curious it looks like this: " + accessToken
51+
+ ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
52+
System.out.println();
53+
54+
// Now let's go and ask for a protected resource!
55+
System.out.println("Now we're going to access a protected resource...");
56+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
57+
service.signRequest(accessToken, request);
58+
final Response response = service.execute(request);
59+
System.out.println("Got it! Lets see what we found...");
60+
System.out.println();
61+
System.out.println(response.getBody());
62+
63+
System.out.println();
64+
System.out.println("That's it man! Go and build something awesome with ScribeJava! :)");
65+
}
66+
}

0 commit comments

Comments
 (0)