Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oSbJson
LOCAL oJson
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect

nSuccess := 0

//  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.
oHttp := CreateObject("Chilkat.Http")

//  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.
oHttp:Login := "SHOPIFY_PRIVATE_API_KEY"
oHttp:Password := "SHOPIFY_PRIVATE_API_SECRET_KEY"
oHttp:BasicAuth := 1

//  Make sure to replace "chilkat" with your store name.
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", "https://chilkat.myshopify.com/admin/products.json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oResp:destroy()
    RETURN
ENDIF

//  Examine the response code.
IF (oResp:StatusCode != 200)
    ? "Received error response code: " + Str(oResp:StatusCode)
    ? "Response body:"
    ? oResp:BodyStr
    oHttp:destroy()
    oResp:destroy()
    RETURN
ENDIF

//  Success.
? "Success."

//  Examine the JSON response 
oSbJson := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbJson)
oJson := CreateObject("Chilkat.JsonObject")
oJson:LoadSb(oSbJson)
oJson:EmitCompact := 0
? oJson:Emit()

//  -------------------------------------------------
//  Now let's do the same using the Chilkat Rest API.
oRest := CreateObject("Chilkat.Rest")

//  Provide the private app credentials:
oRest:SetAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_SECRET_KEY")

//  Connect to the shopify server.
nBTls := 1
nPort := 443
nBAutoReconnect := 1
//  Make sure to replace "chilkat" with your store name.
nSuccess := oRest:Connect("chilkat.myshopify.com", nPort, nBTls, nBAutoReconnect)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oHttp:destroy()
    oResp:destroy()
    oSbJson:destroy()
    oJson:destroy()
    oRest:destroy()
    RETURN
ENDIF

oSbJson:Clear()
nSuccess := oRest:FullRequestNoBodySb("GET", "/admin/products.json", oSbJson)
IF (oRest:LastMethodSuccess == 0)
    ? oRest:LastErrorText
    oHttp:destroy()
    oResp:destroy()
    oSbJson:destroy()
    oJson:destroy()
    oRest:destroy()
    RETURN
ENDIF

IF (oRest:ResponseStatusCode != 200)
    ? "Received error response code: " + Str(oRest:ResponseStatusCode)
    ? "Response body:"
    ? oSbJson:GetAsString()
    oHttp:destroy()
    oResp:destroy()
    oSbJson:destroy()
    oJson:destroy()
    oRest:destroy()
    RETURN
ENDIF

//  Success...
oJson:LoadSb(oSbJson)
? oJson:Emit()

oHttp:destroy()
oResp:destroy()
oSbJson:destroy()
oJson:destroy()
oRest:destroy()