PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkOAuth1.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; See this example for getting an OAuth1 token for Etsy
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(json,"qa_data/tokens/etsy.json")
If success = 0
Debug "Failed to load previously fetched Etsy OAuth1 access token."
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
oauth1.i = CkOAuth1::ckCreate()
If oauth1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkOAuth1::setCkConsumerKey(oauth1, "app_keystring")
CkOAuth1::setCkConsumerSecret(oauth1, "app_shared_secret")
CkOAuth1::setCkToken(oauth1, CkJsonObject::ckStringOf(json,"oauth_token"))
CkOAuth1::setCkTokenSecret(oauth1, CkJsonObject::ckStringOf(json,"oauth_token_secret"))
CkOAuth1::setCkSignatureMethod(oauth1, "HMAC-SHA1")
CkOAuth1::ckGenNonce(oauth1,16)
autoReconnect.i = 1
tls.i = 1
success = CkRest::ckConnect(rest,"openapi.etsy.com",443,tls,autoReconnect)
If success = 0
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkOAuth1::ckDispose(oauth1)
ProcedureReturn
EndIf
; Tell the REST object to use the OAuth1 object.
success = CkRest::ckSetAuthOAuth1(rest,oauth1,1)
jsonText.s = "[{" + Chr(34) + "product_id" + Chr(34) + ":1999949999," + Chr(34) + "property_values" + Chr(34) + ":[]," + Chr(34) + "offerings" + Chr(34) + ":[{" + Chr(34) + "offering_id" + Chr(34) + ":9999905883," + Chr(34) + "price" + Chr(34) + ":" + Chr(34) + "36.23" + Chr(34) + "," + Chr(34) + "quantity" + Chr(34) + ":1}]}]"
CkRest::ckAddQueryParam(rest,"products",jsonText)
CkRest::ckAddHeader(rest,"Content-Type","application/x-www-form-urlencoded")
jsonResponseText.s = CkRest::ckFullRequestFormUrlEncoded(rest,"PUT","/v2/listings/228827035/inventory")
If CkRest::ckLastMethodSuccess(rest) = 0
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkOAuth1::ckDispose(oauth1)
ProcedureReturn
EndIf
jsonResponse.i = CkJsonObject::ckCreate()
If jsonResponse.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoad(jsonResponse,jsonResponseText)
CkJsonObject::setCkEmitCompact(jsonResponse, 0)
Debug CkJsonObject::ckEmit(jsonResponse)
Debug "Response status code: " + Str(CkRest::ckResponseStatusCode(rest))
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkOAuth1::ckDispose(oauth1)
CkJsonObject::ckDispose(jsonResponse)
ProcedureReturn
EndProcedure