Java
Chilkat2 Java
Update an Inventory Listing using OAuth1 Authentication
See more Etsy Examples
Updates an inventory listing. This example uses OAuth1 authentication instead of providing an api_key=MY_ETSY_KEYSTRING query parameter.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.OAuth1;
import com.chilkatsoft.chilkat2.Rest;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Rest rest = new Rest();
// See this example for getting an OAuth1 token for Etsy
JsonObject json = new JsonObject();
try {
json.loadFile("qa_data/tokens/etsy.json");
} catch (ChilkatException e) {
System.out.println("Failed to load previously fetched Etsy OAuth1 access token.");
return;
}
OAuth1 oauth1 = new OAuth1();
oauth1.setConsumerKey("app_keystring");
oauth1.setConsumerSecret("app_shared_secret");
oauth1.setToken(json.stringOf("oauth_token"));
oauth1.setTokenSecret(json.stringOf("oauth_token_secret"));
oauth1.setSignatureMethod("HMAC-SHA1");
oauth1.genNonce(16);
boolean autoReconnect = true;
boolean tls = true;
try {
rest.connect("openapi.etsy.com", 443, tls, autoReconnect);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Tell the REST object to use the OAuth1 object.
rest.setAuthOAuth1(oauth1, true);
String jsonText = "[{\"product_id\":1999949999,\"property_values\":[],\"offerings\":[{\"offering_id\":9999905883,\"price\":\"36.23\",\"quantity\":1}]}]";
rest.addQueryParam("products", jsonText);
rest.addHeader("Content-Type", "application/x-www-form-urlencoded");
String jsonResponseText;
try {
jsonResponseText = rest.fullRequestFormUrlEncoded("PUT", "/v2/listings/228827035/inventory");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject jsonResponse = new JsonObject();
jsonResponse.load(jsonResponseText);
jsonResponse.setEmitCompact(false);
System.out.println(jsonResponse.emit());
System.out.println("Response status code: " + rest.getResponseStatusCode());
}
}