Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.6.0+

Amazon Cognito: Refresh Tokens with Rotation Support

See more Amazon Cognito Examples

Renew a user session without asking for the password again. Choose the default GetTokensFromRefreshToken JSON API or the OAuth refresh_token grant. Both support refresh-token rotation. REFRESH_TOKEN_AUTH is incompatible with rotation and is intentionally not used. With rotation disabled, retain the old refresh token when the response omits a replacement; with rotation enabled, save the new one before another refresh.

Use a public client without a secret, matching the initial sign-in. Managed-login sessions can use the OAuth endpoint. API-origin sessions can use OAuth refresh only when device remembering is disabled; otherwise use the API with the confirmed DeviceKey. This collection's password sign-in example assumes device remembering is disabled. Coordinate simultaneous refreshes and handle the configured retry grace period; expiration or revocation requires a new sign-in. Rotating does not extend the original refresh-token lifetime.

Uses direct Chilkat.Http requests to the regional Cognito User Pools JSON API. Public app-client operations do not use IAM signing. These examples use a public app client without a client secret; do not add administrative AWS credentials. For a confidential app client, implement the operation-specific SecretHash/SECRET_HASH or ClientSecret requirements on a trusted system.

Create qa_data/cognito/app.json with string fields region (for example us-west-2), user_pool_id, client_id, and account_key (your local account identifier). OAuth examples also require domain, the trusted HTTPS base URL of your Cognito domain, without a trailing slash. The region and user pool must match the app client. These examples use commercial AWS endpoints.

Sample cache/input files contain secrets. Use an access-controlled directory outside web content and source control, separate each local user/account, and serialize operations that modify the same cache. In a real application obtain passwords and codes through private UI instead of leaving them in files. Cache writes retain a .bak copy; protect and remove obsolete backups and challenge files after the flow. Production storage should provide atomic replacement and OS-backed secret protection. Never restore an old refresh token automatically after rotation. These examples do not require a hosted web server.

Checks transport completion, HTTP status, and required response fields before using results. Cognito often reports throttling, invalid codes, and authentication failures as HTTP 400. Present generic sign-in/recovery errors to users; keep detailed diagnostics private. Retry throttled reads with bounded backoff, but reconcile uncertain writes and refresh rotations before repeating them.

AWS documentation 1 | AWS documentation 2

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkDateTimeW.h>
#include <C_CkFileAccessW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW app;
    const wchar_t *clientId;
    HCkStringBuilderW endpoint;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW body;
    HCkJsonObjectW result;
    const wchar_t *method;
    HCkJsonObjectW cached;
    HCkStringBuilderW tokenEndpoint;
    HCkHttpRequestW req;
    HCkJsonObjectW token;
    HCkDateTimeW obtained;
    HCkFileAccessW tokenrefreshedusertokens;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    //  App settings are trusted configuration. Keep a separate account_key/cache per local user.
    app = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(app,TRUE);
    success = CkJsonObjectW_LoadFile(app,L"qa_data/cognito/app.json");
    if (success != TRUE) {
        wprintf(L"Could not load Cognito app settings.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"region") != 1) || (CkJsonObjectW_stringOf(app,L"region") == L"")) {
        wprintf(L"Missing or invalid region.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"user_pool_id") != 1) || (CkJsonObjectW_stringOf(app,L"user_pool_id") == L"")) {
        wprintf(L"Missing or invalid user_pool_id.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"client_id") != 1) || (CkJsonObjectW_stringOf(app,L"client_id") == L"")) {
        wprintf(L"Missing or invalid client_id.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"account_key") != 1) || (CkJsonObjectW_stringOf(app,L"account_key") == L"")) {
        wprintf(L"Missing or invalid account_key.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    clientId = CkJsonObjectW_stringOf(app,L"client_id");
    endpoint = CkStringBuilderW_Create();
    CkStringBuilderW_Append(endpoint,L"https://cognito-idp.");
    CkStringBuilderW_Append(endpoint,CkJsonObjectW_stringOf(app,L"region"));
    CkStringBuilderW_Append(endpoint,L".amazonaws.com/");
    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putFollowRedirects(http,FALSE);
    CkHttpW_putConnectTimeout(http,20);
    CkHttpW_putReadTimeout(http,60);
    resp = CkHttpResponseW_Create();
    body = CkJsonObjectW_Create();
    result = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(result,TRUE);
    //  "api" supports API sign-in and refresh rotation. "oauth" uses the Cognito domain.
    method = L"api";
    if ((method != L"api") && (method != L"oauth")) {
        wprintf(L"Choose api or oauth.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    cached = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(cached,TRUE);
    success = CkJsonObjectW_LoadFile(cached,L"qa_data/tokens/cognito-user.json");
    if (success != TRUE) {
        wprintf(L"Could not load qa_data/tokens/cognito-user.json.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(cached,L"client_id") != 1) || (CkJsonObjectW_stringOf(cached,L"client_id") == L"")) {
        wprintf(L"Missing or invalid client_id.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        return;
    }

    if (CkJsonObjectW_stringOf(cached,L"client_id") != CkJsonObjectW_stringOf(app,L"client_id")) {
        wprintf(L"Saved data belongs to another client, pool, or local account.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(cached,L"user_pool_id") != 1) || (CkJsonObjectW_stringOf(cached,L"user_pool_id") == L"")) {
        wprintf(L"Missing or invalid user_pool_id.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        return;
    }

    if (CkJsonObjectW_stringOf(cached,L"user_pool_id") != CkJsonObjectW_stringOf(app,L"user_pool_id")) {
        wprintf(L"Saved data belongs to another client, pool, or local account.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(cached,L"account_key") != 1) || (CkJsonObjectW_stringOf(cached,L"account_key") == L"")) {
        wprintf(L"Missing or invalid account_key.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        return;
    }

    if (CkJsonObjectW_stringOf(cached,L"account_key") != CkJsonObjectW_stringOf(app,L"account_key")) {
        wprintf(L"Saved data belongs to another client, pool, or local account.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(cached,L"refresh_token") != 1) || (CkJsonObjectW_stringOf(cached,L"refresh_token") == L"")) {
        wprintf(L"Missing or invalid refresh_token.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        return;
    }

    if (method == L"api") {
        CkJsonObjectW_UpdateString(body,L"ClientId",clientId);
        CkJsonObjectW_UpdateString(body,L"RefreshToken",CkJsonObjectW_stringOf(cached,L"refresh_token"));
        //  If device remembering is enabled, supply the confirmed DeviceKey from your device flow.
        if ((CkJsonObjectW_JsonTypeOf(cached,L"device_key") == 1) && (CkJsonObjectW_stringOf(cached,L"device_key") != L"")) {
            CkJsonObjectW_UpdateString(body,L"DeviceKey",CkJsonObjectW_stringOf(cached,L"device_key"));
        }

        CkHttpW_SetRequestHeader(http,L"X-Amz-Target",L"AWSCognitoIdentityProviderService.GetTokensFromRefreshToken");
        success = CkHttpW_HttpJson(http,L"POST",CkStringBuilderW_getAsString(endpoint),body,L"application/x-amz-json-1.0",resp);
        if (CkHttpResponseW_getStatusCode(resp) != 200) {
            wprintf(L"HTTP status: %d\n",CkHttpResponseW_getStatusCode(resp));
            success = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
            if (success == TRUE) {
                wprintf(L"Error type: %s\n",CkJsonObjectW_stringOf(result,L"__type"));
                wprintf(L"OAuth error: %s\n",CkJsonObjectW_stringOf(result,L"error"));
            }

            //  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            return;
        }

        if (success != TRUE) {
            wprintf(L"The HTTP exchange did not complete.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            return;
        }

        success = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
        if (success != TRUE) {
            wprintf(L"Expected a JSON response.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            return;
        }

    }
    else {
        if ((CkJsonObjectW_JsonTypeOf(app,L"domain") != 1) || (CkJsonObjectW_stringOf(app,L"domain") == L"")) {
            wprintf(L"Missing or invalid domain.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            return;
        }

        //  This URL comes from trusted configuration, never from a token or user-supplied URL.
        tokenEndpoint = CkStringBuilderW_Create();
        CkStringBuilderW_Append(tokenEndpoint,CkJsonObjectW_stringOf(app,L"domain"));
        CkStringBuilderW_Append(tokenEndpoint,L"/oauth2/token");
        req = CkHttpRequestW_Create();
        CkHttpRequestW_putHttpVerb(req,L"POST");
        CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded");
        CkHttpRequestW_AddParam(req,L"grant_type",L"refresh_token");
        CkHttpRequestW_AddParam(req,L"client_id",clientId);
        CkHttpRequestW_AddParam(req,L"refresh_token",CkJsonObjectW_stringOf(cached,L"refresh_token"));
        success = CkHttpW_HttpReq(http,CkStringBuilderW_getAsString(tokenEndpoint),req,resp);
        if (CkHttpResponseW_getStatusCode(resp) != 200) {
            wprintf(L"HTTP status: %d\n",CkHttpResponseW_getStatusCode(resp));
            success = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
            if (success == TRUE) {
                wprintf(L"Error type: %s\n",CkJsonObjectW_stringOf(result,L"__type"));
                wprintf(L"OAuth error: %s\n",CkJsonObjectW_stringOf(result,L"error"));
            }

            //  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            CkStringBuilderW_Dispose(tokenEndpoint);
            CkHttpRequestW_Dispose(req);
            return;
        }

        if (success != TRUE) {
            wprintf(L"The HTTP exchange did not complete.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            CkStringBuilderW_Dispose(tokenEndpoint);
            CkHttpRequestW_Dispose(req);
            return;
        }

        success = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
        if (success != TRUE) {
            wprintf(L"Expected a JSON response.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            CkStringBuilderW_Dispose(tokenEndpoint);
            CkHttpRequestW_Dispose(req);
            return;
        }

    }

    token = CkJsonObjectW_Create();
    if (method == L"api") {
        CkJsonObjectW_UpdateString(token,L"access_token",CkJsonObjectW_stringOf(result,L"AuthenticationResult.AccessToken"));
        CkJsonObjectW_UpdateString(token,L"id_token",CkJsonObjectW_stringOf(result,L"AuthenticationResult.IdToken"));
        CkJsonObjectW_UpdateString(token,L"token_type",CkJsonObjectW_stringOf(result,L"AuthenticationResult.TokenType"));
        CkJsonObjectW_UpdateInt(token,L"expires_in",CkJsonObjectW_IntOf(result,L"AuthenticationResult.ExpiresIn"));
        if (CkJsonObjectW_HasMember(result,L"AuthenticationResult.RefreshToken") == TRUE) {
            CkJsonObjectW_UpdateString(token,L"refresh_token",CkJsonObjectW_stringOf(result,L"AuthenticationResult.RefreshToken"));
        }

    }
    else {
        success = CkJsonObjectW_Load(token,CkJsonObjectW_emit(result));
        if (success != TRUE) {
            wprintf(L"Could not copy the token response.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            CkStringBuilderW_Dispose(tokenEndpoint);
            CkHttpRequestW_Dispose(req);
            CkJsonObjectW_Dispose(token);
            return;
        }

    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"access_token") != 1) || (CkJsonObjectW_stringOf(token,L"access_token") == L"")) {
        wprintf(L"Missing or invalid access_token.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"id_token") != 1) || (CkJsonObjectW_stringOf(token,L"id_token") == L"")) {
        wprintf(L"Missing or invalid id_token.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"expires_in") != 2) || (CkJsonObjectW_IntOf(token,L"expires_in") <= 0)) {
        wprintf(L"Expected a positive token lifetime.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(token);
        return;
    }

    if (CkJsonObjectW_stringOf(token,L"token_type") != L"Bearer") {
        wprintf(L"Expected Bearer tokens.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(token);
        return;
    }

    //  Rotation enabled: save the replacement. Rotation disabled: retain the old refresh token.
    if (CkJsonObjectW_HasMember(token,L"refresh_token") == TRUE) {
        if ((CkJsonObjectW_JsonTypeOf(token,L"refresh_token") != 1) || (CkJsonObjectW_stringOf(token,L"refresh_token") == L"")) {
            wprintf(L"Missing or invalid refresh_token.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            CkStringBuilderW_Dispose(tokenEndpoint);
            CkHttpRequestW_Dispose(req);
            CkJsonObjectW_Dispose(token);
            return;
        }

    }
    else {
        CkJsonObjectW_UpdateString(token,L"refresh_token",CkJsonObjectW_stringOf(cached,L"refresh_token"));
    }

    if ((CkJsonObjectW_JsonTypeOf(cached,L"device_key") == 1) && (CkJsonObjectW_stringOf(cached,L"device_key") != L"")) {
        CkJsonObjectW_UpdateString(token,L"device_key",CkJsonObjectW_stringOf(cached,L"device_key"));
    }

    CkJsonObjectW_UpdateString(token,L"client_id",CkJsonObjectW_stringOf(app,L"client_id"));
    CkJsonObjectW_UpdateString(token,L"user_pool_id",CkJsonObjectW_stringOf(app,L"user_pool_id"));
    CkJsonObjectW_UpdateString(token,L"account_key",CkJsonObjectW_stringOf(app,L"account_key"));
    obtained = CkDateTimeW_Create();
    success = CkDateTimeW_SetFromCurrentSystemTime(obtained);
    if (success != TRUE) {
        wprintf(L"Could not get the acquisition time.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(token);
        CkDateTimeW_Dispose(obtained);
        return;
    }

    CkJsonObjectW_UpdateString(token,L"obtained_at",CkDateTimeW_getAsTimestamp(obtained,FALSE));
    tokenrefreshedusertokens = CkFileAccessW_Create();
    success = CkFileAccessW_DirEnsureExists(tokenrefreshedusertokens,L"qa_data/tokens");
    if (success != TRUE) {
        wprintf(L"Could not create the private cache directory.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(token);
        CkDateTimeW_Dispose(obtained);
        CkFileAccessW_Dispose(tokenrefreshedusertokens);
        return;
    }

    if (CkFileAccessW_FileExists(tokenrefreshedusertokens,L"qa_data/tokens/cognito-user.json") == TRUE) {
        success = CkFileAccessW_FileCopy(tokenrefreshedusertokens,L"qa_data/tokens/cognito-user.json",L"qa_data/tokens/cognito-user.json.bak",FALSE);
        if (success != TRUE) {
            wprintf(L"Could not back up the existing cache.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(cached);
            CkStringBuilderW_Dispose(tokenEndpoint);
            CkHttpRequestW_Dispose(req);
            CkJsonObjectW_Dispose(token);
            CkDateTimeW_Dispose(obtained);
            CkFileAccessW_Dispose(tokenrefreshedusertokens);
            return;
        }

    }

    success = CkJsonObjectW_WriteFile(token,L"qa_data/tokens/cognito-user.json");
    if (success != TRUE) {
        wprintf(L"Could not save refreshed user tokens; preserve any previous backup.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(cached);
        CkStringBuilderW_Dispose(tokenEndpoint);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(token);
        CkDateTimeW_Dispose(obtained);
        CkFileAccessW_Dispose(tokenrefreshedusertokens);
        return;
    }

    wprintf(L"Tokens refreshed; private cache updated.\n");


    CkJsonObjectW_Dispose(app);
    CkStringBuilderW_Dispose(endpoint);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(body);
    CkJsonObjectW_Dispose(result);
    CkJsonObjectW_Dispose(cached);
    CkStringBuilderW_Dispose(tokenEndpoint);
    CkHttpRequestW_Dispose(req);
    CkJsonObjectW_Dispose(token);
    CkDateTimeW_Dispose(obtained);
    CkFileAccessW_Dispose(tokenrefreshedusertokens);

    }