Java
Java
Shopify Private Authentication for Private Apps
See more Shopify Examples
Shopify private authentication is for interacting with your own store through private applications. It uses HTTP "Basic" authentication with your Shopify private application key and secret key.This example demonstrates how to send a private authenticated request using Chilkat Http, and then the same using Chilkat Rest.
Chilkat Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First demonstrate sending a simple request using Shopify private authentication w/ the Chilkat Http API.
CkHttp http = new CkHttp();
// To use HTTP Basic Authentication with any HTTP request, we simply set the Login, Password, and BasicAuth properties.
// Important: All HTTP requests using Basic authentication must be over SSL/TLS.
http.put_Login("SHOPIFY_PRIVATE_API_KEY");
http.put_Password("SHOPIFY_PRIVATE_API_SECRET_KEY");
http.put_BasicAuth(true);
// Make sure to replace "chilkat" with your store name.
CkHttpResponse resp = new CkHttpResponse();
success = http.HttpNoBody("GET","https://chilkat.myshopify.com/admin/products.json",resp);
if (success == false) {
System.out.println(http.lastErrorText());
return;
}
// Examine the response code.
if (resp.get_StatusCode() != 200) {
System.out.println("Received error response code: " + resp.get_StatusCode());
System.out.println("Response body:");
System.out.println(resp.bodyStr());
return;
}
// Success.
System.out.println("Success.");
// Examine the JSON response
CkStringBuilder sbJson = new CkStringBuilder();
resp.GetBodySb(sbJson);
CkJsonObject json = new CkJsonObject();
json.LoadSb(sbJson);
json.put_EmitCompact(false);
System.out.println(json.emit());
// -------------------------------------------------
// Now let's do the same using the Chilkat Rest API.
CkRest rest = new CkRest();
// Provide the private app credentials:
rest.SetAuthBasic("SHOPIFY_PRIVATE_API_KEY","SHOPIFY_PRIVATE_API_SECRET_KEY");
// Connect to the shopify server.
boolean bTls = true;
int port = 443;
boolean bAutoReconnect = true;
// Make sure to replace "chilkat" with your store name.
success = rest.Connect("chilkat.myshopify.com",port,bTls,bAutoReconnect);
if (success == false) {
System.out.println(rest.lastErrorText());
return;
}
sbJson.Clear();
success = rest.FullRequestNoBodySb("GET","/admin/products.json",sbJson);
if (rest.get_LastMethodSuccess() == false) {
System.out.println(rest.lastErrorText());
return;
}
if (rest.get_ResponseStatusCode() != 200) {
System.out.println("Received error response code: " + rest.get_ResponseStatusCode());
System.out.println("Response body:");
System.out.println(sbJson.getAsString());
return;
}
// Success...
json.LoadSb(sbJson);
System.out.println(json.emit());
}
}