Unicode C
Unicode C
Amazon Cognito - List Users
See more Amazon Cognito Examples
Lists the users in the Amazon Cognito user pool.Chilkat Unicode C Downloads
#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkRestW rest;
HCkAuthAwsW authAws;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkJsonObjectW json;
HCkStringBuilderW sbRequestBody;
HCkStringBuilderW sbResponseBody;
int respStatusCode;
HCkJsonObjectW jsonResponse;
BOOL Enabled;
const wchar_t *UserCreateDate;
const wchar_t *UserLastModifiedDate;
const wchar_t *UserStatus;
const wchar_t *Username;
int j;
int count_j;
const wchar_t *Name;
const wchar_t *Value;
int i;
int count_i;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest = CkRestW_Create();
authAws = CkAuthAwsW_Create();
CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY");
// Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
CkAuthAwsW_putRegion(authAws,L"us-west-2");
CkAuthAwsW_putServiceName(authAws,L"cognito-idp");
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
CkRestW_SetAuthAws(rest,authAws);
// URL: https://cognito-idp.us-west-2.amazonaws.com/
bTls = TRUE;
port = 443;
bAutoReconnect = TRUE;
// Use the same region as specified above.
success = CkRestW_Connect(rest,L"cognito-idp.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
if (success != TRUE) {
wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
return;
}
// Note: The above code does not need to be repeatedly called for each REST request.
// The rest object can be setup once, and then many requests can be sent. Chilkat will automatically
// reconnect within a FullRequest* method as needed. It is only the very first connection that is explicitly
// made via the Connect method.
// The following JSON is sent in the request body.
// {
// "UserPoolId": "us-west-2_yt6WzO3SA"
// }
//
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"UserPoolId",L"us-west-2_yt6WzO3SA");
CkRestW_AddHeader(rest,L"Content-Type",L"application/x-amz-json-1.0");
CkRestW_AddHeader(rest,L"X-Amz-Target",L"AWSCognitoIdentityProviderService.ListUsers");
CkRestW_AddHeader(rest,L"Accept-Encoding",L"identity");
sbRequestBody = CkStringBuilderW_Create();
CkJsonObjectW_EmitSb(json,sbRequestBody);
sbResponseBody = CkStringBuilderW_Create();
success = CkRestW_FullRequestSb(rest,L"POST",L"/",sbRequestBody,sbResponseBody);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
respStatusCode = CkRestW_getResponseStatusCode(rest);
wprintf(L"response status code = %d\n",respStatusCode);
if (respStatusCode != 200) {
wprintf(L"Response Status Code = %d\n",respStatusCode);
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkRestW_responseHeader(rest));
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
jsonResponse = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jsonResponse,sbResponseBody);
CkJsonObjectW_putEmitCompact(jsonResponse,FALSE);
wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse));
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "Users": [
// {
// "Attributes": [
// {
// "Name": "sub",
// "Value": "1874cf28-00d3-4ac1-a24a-68dcb0cecf09"
// },
// {
// "Name": "email_verified",
// "Value": "true"
// },
// {
// "Name": "phone_number_verified",
// "Value": "true"
// },
// {
// "Name": "phone_number",
// "Value": "+16302581871"
// },
// {
// "Name": "email",
// "Value": "admin@chilkatsoft.com"
// }
// ],
// "Enabled": true,
// "UserCreateDate": 1.636404386657E9,
// "UserLastModifiedDate": 1.636404386657E9,
// "UserStatus": "FORCE_CHANGE_PASSWORD",
// "Username": "matt"
// },
// {
// . . .
// }
// ]
// }
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"Users");
while (i < count_i) {
CkJsonObjectW_putI(jsonResponse,i);
Enabled = CkJsonObjectW_BoolOf(jsonResponse,L"Users[i].Enabled");
UserCreateDate = CkJsonObjectW_stringOf(jsonResponse,L"Users[i].UserCreateDate");
UserLastModifiedDate = CkJsonObjectW_stringOf(jsonResponse,L"Users[i].UserLastModifiedDate");
UserStatus = CkJsonObjectW_stringOf(jsonResponse,L"Users[i].UserStatus");
Username = CkJsonObjectW_stringOf(jsonResponse,L"Users[i].Username");
j = 0;
count_j = CkJsonObjectW_SizeOfArray(jsonResponse,L"Users[i].Attributes");
while (j < count_j) {
CkJsonObjectW_putJ(jsonResponse,j);
Name = CkJsonObjectW_stringOf(jsonResponse,L"Users[i].Attributes[j].Name");
Value = CkJsonObjectW_stringOf(jsonResponse,L"Users[i].Attributes[j].Value");
j = j + 1;
}
i = i + 1;
}
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jsonResponse);
}