Sample code for 30+ languages & platforms
C

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

C
#include <C_CkRest.h>
#include <C_CkJsonObject.h>
#include <C_CkOAuth1.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    HCkJsonObject json;
    HCkOAuth1 oauth1;
    BOOL autoReconnect;
    BOOL tls;
    const char *jsonText;
    const char *jsonResponseText;
    HCkJsonObject jsonResponse;

    success = FALSE;

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

    rest = CkRest_Create();

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

    json = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(json,"qa_data/tokens/etsy.json");
    if (success == FALSE) {
        printf("Failed to load previously fetched Etsy OAuth1 access token.\n");
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(json);
        return;
    }

    oauth1 = CkOAuth1_Create();

    CkOAuth1_putConsumerKey(oauth1,"app_keystring");
    CkOAuth1_putConsumerSecret(oauth1,"app_shared_secret");
    CkOAuth1_putToken(oauth1,CkJsonObject_stringOf(json,"oauth_token"));
    CkOAuth1_putTokenSecret(oauth1,CkJsonObject_stringOf(json,"oauth_token_secret"));
    CkOAuth1_putSignatureMethod(oauth1,"HMAC-SHA1");
    CkOAuth1_GenNonce(oauth1,16);

    autoReconnect = TRUE;
    tls = TRUE;
    success = CkRest_Connect(rest,"openapi.etsy.com",443,tls,autoReconnect);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(json);
        CkOAuth1_Dispose(oauth1);
        return;
    }

    // Tell the REST object to use the OAuth1 object.
    success = CkRest_SetAuthOAuth1(rest,oauth1,TRUE);

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

    CkRest_AddQueryParam(rest,"products",jsonText);
    CkRest_AddHeader(rest,"Content-Type","application/x-www-form-urlencoded");

    jsonResponseText = CkRest_fullRequestFormUrlEncoded(rest,"PUT","/v2/listings/228827035/inventory");
    if (CkRest_getLastMethodSuccess(rest) == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(json);
        CkOAuth1_Dispose(oauth1);
        return;
    }

    jsonResponse = CkJsonObject_Create();
    CkJsonObject_Load(jsonResponse,jsonResponseText);
    CkJsonObject_putEmitCompact(jsonResponse,FALSE);

    printf("%s\n",CkJsonObject_emit(jsonResponse));

    printf("Response status code: %d\n",CkRest_getResponseStatusCode(rest));


    CkRest_Dispose(rest);
    CkJsonObject_Dispose(json);
    CkOAuth1_Dispose(oauth1);
    CkJsonObject_Dispose(jsonResponse);

    }