Sample code for 30+ languages & platforms
C

Create Restricted Data Token (RDT)

See more Amazon SP-API Examples

Returns a Restricted Data Token (RDT) for one or more restricted resources that you specify.

Chilkat C Downloads

C
#include <C_CkAuthAws.h>
#include <C_CkRest.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkAuthAws authAws;
    HCkRest rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkJsonObject jsonToken;
    const char *lwa_token;
    HCkJsonObject json;
    HCkStringBuilder sbRequest;
    HCkStringBuilder sbResponse;
    const char *uri;
    int statusCode;
    HCkJsonObject jsonResp;
    int expiresIn;
    const char *restrictedDataToken;

    success = FALSE;

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

    authAws = CkAuthAws_Create();
    CkAuthAws_putAccessKey(authAws,"AWS_ACCESS_KEY");
    CkAuthAws_putSecretKey(authAws,"AWS_SECRET_KEY");
    CkAuthAws_putServiceName(authAws,"execute-api");
    // Use the region that is correct for your needs.
    CkAuthAws_putRegion(authAws,"eu-west-1");

    rest = CkRest_Create();
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    // The sandbox endpoint (sandbox.sellingpartnerapi-eu.amazon.com) fails.
    // Use the production endpoint and see the note below.
    success = CkRest_Connect(rest,"sellingpartnerapi-eu.amazon.com",port,bTls,bAutoReconnect);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkAuthAws_Dispose(authAws);
        CkRest_Dispose(rest);
        return;
    }

    success = CkRest_SetAuthAws(rest,authAws);

    // Load the previously obtained LWA access token.
    // See Fetch SP-API LWA Access Token
    jsonToken = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(jsonToken,"qa_data/tokens/sp_api_lwa_token.json");
    if (success == FALSE) {
        printf("Failed to load LWA access token.\n");
        CkAuthAws_Dispose(authAws);
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(jsonToken);
        return;
    }

    // Add the x-amz-access-token request header.
    lwa_token = CkJsonObject_stringOf(jsonToken,"access_token");
    CkRest_ClearAllHeaders(rest);
    CkRest_AddHeader(rest,"x-amz-access-token",lwa_token);

    // We're going to send a POST with the following JSON body:

    // {
    //   "restrictedResources": [
    //     {
    //       "method": "GET",
    //       "path": "/orders/v0/orders",
    //       "dataElements": ["buyerInfo", "shippingAddress"]
    //     }
    //   ]
    // }

    // Use this online tool to generate code from sample JSON: 
    // Generate Code to Create JSON

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"restrictedResources[0].method","GET");
    CkJsonObject_UpdateString(json,"restrictedResources[0].path","/orders/v0/orders");
    CkJsonObject_UpdateString(json,"restrictedResources[0].dataElements[0]","buyerInfo");
    CkJsonObject_UpdateString(json,"restrictedResources[0].dataElements[1]","shippingAddress");

    sbRequest = CkStringBuilder_Create();
    CkJsonObject_EmitSb(json,sbRequest);

    sbResponse = CkStringBuilder_Create();
    uri = "/tokens/2021-03-01/restrictedDataToken";
    success = CkRest_FullRequestSb(rest,"POST",uri,sbRequest,sbResponse);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkAuthAws_Dispose(authAws);
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(jsonToken);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequest);
        CkStringBuilder_Dispose(sbResponse);
        return;
    }

    // ------------------------------------------------------------------------------------
    // Note: Using the sandbox endpoint, such as sandbox.sellingpartnerapi-eu.amazon.com
    // results in a response status code of 400 with the following error:
    // [{"code":"InvalidInput","message":"Could not match input arguments"}]
    // Getting a restricted data token seems to only work with the production endpoint.
    // ------------------------------------------------------------------------------------

    // Examine the response status.
    statusCode = CkRest_getResponseStatusCode(rest);
    if (statusCode != 200) {
        printf("Response status code: %d\n",statusCode);
        printf("Response status text: %s\n",CkRest_responseStatusText(rest));
        printf("Response body: \n");
        printf("%s\n",CkStringBuilder_getAsString(sbResponse));
        printf("Failed.\n");
        CkAuthAws_Dispose(authAws);
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(jsonToken);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequest);
        CkStringBuilder_Dispose(sbResponse);
        return;
    }

    printf("%s\n",CkStringBuilder_getAsString(sbResponse));

    // If successful, gets a JSON response such as the following:

    // {
    //   "expiresIn": 3600,
    //   "restrictedDataToken": "Atz.sprdt|AYAB.....TQ="
    // }

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    jsonResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jsonResp,sbResponse);

    expiresIn = CkJsonObject_IntOf(jsonResp,"expiresIn");
    restrictedDataToken = CkJsonObject_stringOf(jsonResp,"restrictedDataToken");

    // Save the RDT for subsequent use..
    success = CkJsonObject_WriteFile(jsonResp,"qa_data/tokens/sp_api_rdt_token.json");

    printf("Success!\n");


    CkAuthAws_Dispose(authAws);
    CkRest_Dispose(rest);
    CkJsonObject_Dispose(jsonToken);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbRequest);
    CkStringBuilder_Dispose(sbResponse);
    CkJsonObject_Dispose(jsonResp);

    }