Skip to content

Commit

Permalink
v3.0.0013
Browse files Browse the repository at this point in the history
Added more authentication methods.
  • Loading branch information
cmunden committed Jun 24, 2023
1 parent d095ffb commit f9291d9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 22 deletions.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,49 @@ out of the gate. There's nothing to change, unless you need a specific feature
implementations (ex. a different Http transport).

### Installation
Version 3.0.0012 of JEtsy is available from the Maven Central Repository [here](https://search.maven.org/search?q=g:com.notronix%20a:JEtsy)
Version 3.0.0013 of JEtsy is available from the Maven Central Repository [here](https://search.maven.org/search?q=g:com.notronix%20a:JEtsy)

<dependency>
<groupId>com.notronix</groupId>
<artifactId>JEtsy</artifactId>
<version>3.0.0012</version>
<version>3.0.0013</version>
</dependency>

### Usage (TODO: update to V3 implementation with OAuth2.0)
JEtsy is intended to be used as a way to allow any Java developer to quickly get their own application (known as the
"Client") off the ground and running. As a result, the first step required is to register your own application with
Etsy. This registration process will provide you with both a "key string" and a "shared secret" (known as the "Client
Credentials"). The client credentials can then be used with JEtsy to obtain access credentials and to authorize the use
of your application with an existing Etsy account. The following steps illustrate how to get started.
Etsy. This registration process will provide you with a "key string" (known as the "App Key"). The App Key can then
be used with JEtsy to make API calls. However, some API calls require you to obtain an access token which can also
be obtained using JEtsy. The following steps illustrate how to get started.

1. Register your Java application with Etsy [here](https://www.etsy.com/developers/documentation/getting_started/register).
1. Register your application with Etsy [here](https://www.etsy.com/developers/documentation/getting_started/register).


2. Record the "key string" and "shared secret" provided by completing step 1 above.
2. Record the "key string" provided by completing step 1 above.


3. Create an instance of `EtsyDataService` using the information from step 2 above.
3. Create an instance of `EtsyAPI` and `AppKey` using the key string from step 2.


`EtsyDataService etsyDataService = new EtsyDataService("YourKeyString", "YourSharedSecret");`
```
EtsyAPI etsyAPI = EtsyAPI.getDefaultInstance();
AppKey appKey = AppKey.forKeyString("your_app_keystring");
4. Use the `etsyDataService` to make API calls that do not require OAuth authentication.
4. Use the `etsyAPI` to make API calls that do not require an access token.
`List<EtsyApiMethod> methods = etsyDataService.getMethodTable();`
```
PingMethod<HttpContent> pingMethod = etsyAPI.getAuthResource().createPingMethod();
Long pingResponse = etsyAPI.execute(appKey, pingMethod).getContent();

4. To make API calls that require OAuth authentication, obtain your temporary OAuth credentials. These credentials will
provide you with a login URL that can be used in any browser to grant your application (client) access to an existing
Etsy account. <br/><br/>*Note that the example below uses an out of band (oob) oauth process. As a result, Etsy will
display the verifier required in the next step. If you provide a callback URL of your own, Etsy will redirect to your
provided URL and specify your verifier token.*
4. To make API calls that require an access token, obtain your token using the OAuth flow.


```
Set<String> scopes = Arrays.stream(EtsyScope.values()).map(Enum::name).collect(toSet());
Credentials temporaryCreds = etsyDataService.getTemporaryCredentials(scopes, "oob");
String loginUrl = temporaryCreds.getLoginUrl();
OAuthConnector connector = etsyAPI.getAuthResource().createOAuthConnector();
String loginURL = connector.getConnectionURL(appKey, "redirect",
Arrays.stream(EtsyScope.values()).collect(Collectors.toSet()));
5. Visit Etsy via the login URL obtained in step 4 above. Etsy will require you to log in to an existing Etsy account.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.notronix'
version '3.0.0012'
version '3.0.0013'

sourceCompatibility = 1.8

Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/notronix/etsy/impl/EtsyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.notronix.etsy.api.*;
import com.notronix.etsy.api.common.method.Method;
import com.notronix.etsy.api.common.method.OAuthMethod;
import com.notronix.etsy.impl.authentication.method.EtsyAuthResource;
import com.notronix.etsy.impl.common.json.InstantAdapter;
import com.notronix.etsy.impl.listings.method.EtsyListingResource;
import com.notronix.etsy.impl.shops.method.EtsyShopResource;
import com.notronix.etsy.impl.taxonomy.method.EtsyTaxonomyResource;
import com.notronix.etsy.impl.users.method.EtsyUserResource;

import java.lang.reflect.Type;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -23,6 +30,7 @@
public class EtsyAPI implements API<HttpContent>
{
private final Object lock = new Object();
private static final EtsyAPI DEFAULT_INSTANCE = createDefaultInstance();
private HttpRequestFactory requestFactory;
private Marshaller marshaller;
private Unmarshaller unmarshaller;
Expand Down Expand Up @@ -216,4 +224,67 @@ public T getContent() {
private <T> T getHeaderValue(HttpHeaders headers, Function<HttpHeaders, T> function) {
return headers == null ? null : function.apply(headers);
}

public static EtsyAPI getDefaultInstance() {
return DEFAULT_INSTANCE;
}

private static EtsyAPI createDefaultInstance() {
Marshaller marshaller = createDefaultMarshaller();
Unmarshaller unmarshaller = createDefaultUnMarshaller();

EtsyAPI etsyAPI = new EtsyAPI();
etsyAPI.setMarshaller(marshaller);
etsyAPI.setUnmarshaller(unmarshaller);
etsyAPI.setAuthResource(new EtsyAuthResource());
etsyAPI.setListingResource(new EtsyListingResource());
etsyAPI.setShopResource(new EtsyShopResource());
etsyAPI.setUserResource(new EtsyUserResource());
etsyAPI.setTaxonomyResource(new EtsyTaxonomyResource());

return etsyAPI;
}

private static Marshaller createDefaultMarshaller() {
return new Marshaller()
{
private final Gson gson = new GsonBuilder().setVersion(0).create();

@Override
public String marshal(Object object) {
return gson.toJson(object);
}
};
}

private static Unmarshaller createDefaultUnMarshaller() {
return new Unmarshaller()
{
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(Instant.class, new InstantAdapter().nullSafe())
.create();

@Override
public <T> T unmarshal(String payload, Class<T> classOfT) {
return gson.fromJson(payload, classOfT);
}

@Override
public <T> T unmarshal(String payload, Type typeOfT) {
return gson.fromJson(payload, typeOfT);
}
};
}

private static void getTime() {
try {
String time = "Sat, 10 Jun 2023 11:36:46 GMT";
Instant date = ZonedDateTime.parse(time, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant();

System.exit(0);
}
catch (Exception exception) {
exception.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.api.client.http.HttpContent;
import com.notronix.etsy.api.AppKey;
import com.notronix.etsy.api.authentication.method.AuthResource;
import com.notronix.etsy.api.authentication.method.GetAccessTokenMethod;
import com.notronix.etsy.api.authentication.model.AccessToken;
import com.notronix.etsy.api.authentication.model.LegacyToken;
import com.notronix.etsy.api.authentication.model.OAuthConnector;
Expand All @@ -28,7 +27,7 @@ public OAuthConnector createOAuthConnector() {
}

@Override
public GetAccessTokenMethod<HttpContent> createGetAccessTokenMethod(AppKey appKey) {
public EtsyGetAccessTokenMethod createGetAccessTokenMethod(AppKey appKey) {
return new EtsyGetAccessTokenMethod().withAppKey(appKey);
}

Expand Down

0 comments on commit f9291d9

Please sign in to comment.