Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let rest = CkoRest()!

    // See this example for getting an OAuth1 token for Etsy

    let json = CkoJsonObject()!
    success = json.loadFile(path: "qa_data/tokens/etsy.json")
    if success == false {
        print("Failed to load previously fetched Etsy OAuth1 access token.")
        return
    }

    let oauth1 = CkoOAuth1()!

    oauth1.consumerKey = "app_keystring"
    oauth1.consumerSecret = "app_shared_secret"
    oauth1.token = json.string(of: "oauth_token")
    oauth1.tokenSecret = json.string(of: "oauth_token_secret")
    oauth1.signatureMethod = "HMAC-SHA1"
    oauth1.genNonce(numBytes: 16)

    var autoReconnect: Bool = true
    var tls: Bool = true
    success = rest.connect(hostname: "openapi.etsy.com", port: 443, tls: tls, autoReconnect: autoReconnect)
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    // Tell the REST object to use the OAuth1 object.
    success = rest.setAuthOAuth1(authProvider: oauth1, useQueryParams: true)

    var jsonText: String? = "[{\"product_id\":1999949999,\"property_values\":[],\"offerings\":[{\"offering_id\":9999905883,\"price\":\"36.23\",\"quantity\":1}]}]"

    rest.addQueryParam(name: "products", value: jsonText)
    rest.addHeader(name: "Content-Type", value: "application/x-www-form-urlencoded")

    var jsonResponseText: String? = rest.fullRequestFormUrlEncoded(httpVerb: "PUT", uriPath: "/v2/listings/228827035/inventory")
    if rest.lastMethodSuccess == false {
        print("\(rest.lastErrorText!)")
        return
    }

    let jsonResponse = CkoJsonObject()!
    jsonResponse.load(json: jsonResponseText)
    jsonResponse.emitCompact = false

    print("\(jsonResponse.emit()!)")

    print("Response status code: \(rest.responseStatusCode.intValue)")

}