Objective-C
Objective-C
Amazon Cognito - Admin Create User
See more Amazon Cognito Examples
Creates a new user in the specified user pool.Chilkat Objective-C Downloads
#import <CkoRest.h>
#import <CkoAuthAws.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>
#import <NSString.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoRest *rest = [[CkoRest alloc] init];
CkoAuthAws *authAws = [[CkoAuthAws alloc] init];
authAws.AccessKey = @"AWS_ACCESS_KEY";
authAws.SecretKey = @"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.)
authAws.Region = @"us-west-2";
authAws.ServiceName = @"cognito-idp";
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
[rest SetAuthAws: authAws];
// URL: https://cognito-idp.us-west-2.amazonaws.com/
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
// Use the same region as specified above.
success = [rest Connect: @"cognito-idp.us-west-2.amazonaws.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];
if (success != YES) {
NSLog(@"%@%d",@"ConnectFailReason: ",[rest.ConnectFailReason intValue]);
NSLog(@"%@",rest.LastErrorText);
return;
}
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "DesiredDeliveryMediums" : [ "SMS", "EMAIL" ],
// "UserAttributes": [
// {
// "Name": "phone_number",
// "Value": "+16302581871"
// },
// {
// "Name": "email",
// "Value": "admin@chilkatsoft.com"
// }
// ],
// "UserPoolId": "us-west-2_yt6WzO3SA",
// "Username": "Matt"
// }
//
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"DesiredDeliveryMediums[0]" value: @"SMS"];
[json UpdateString: @"DesiredDeliveryMediums[1]" value: @"EMAIL"];
[json UpdateString: @"UserAttributes[0].Name" value: @"phone_number"];
[json UpdateString: @"UserAttributes[0].Value" value: @"+16302581871"];
[json UpdateString: @"UserAttributes[1].Name" value: @"email"];
[json UpdateString: @"UserAttributes[1].Value" value: @"admin@chilkatsoft.com"];
[json UpdateString: @"UserPoolId" value: @"us-west-2_yt6WzO3SA"];
[json UpdateString: @"Username" value: @"Matt"];
[rest AddHeader: @"Content-Type" value: @"application/x-amz-json-1.0"];
[rest AddHeader: @"X-Amz-Target" value: @"AWSCognitoIdentityProviderService.AdminCreateUser"];
[rest AddHeader: @"Accept-Encoding" value: @"identity"];
CkoStringBuilder *sbRequestBody = [[CkoStringBuilder alloc] init];
[json EmitSb: sbRequestBody];
CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestSb: @"POST" uriPath: @"/" requestBody: sbRequestBody responseBody: sbResponseBody];
if (success != YES) {
NSLog(@"%@",rest.LastErrorText);
return;
}
int respStatusCode = [rest.ResponseStatusCode intValue];
NSLog(@"%@%d",@"response status code = ",respStatusCode);
if (respStatusCode != 200) {
NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
NSLog(@"%@",@"Response Header:");
NSLog(@"%@",rest.ResponseHeader);
NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[sbResponseBody GetAsString]);
return;
}
CkoJsonObject *jsonResponse = [[CkoJsonObject alloc] init];
[jsonResponse LoadSb: sbResponseBody];
jsonResponse.EmitCompact = NO;
NSLog(@"%@",[jsonResponse Emit]);
// 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
// {
// "User": {
// "Attributes": [
// {
// "Name": "sub",
// "Value": "dcd3db0c-32cf-4a33-a6b2-173653f7c0e7"
// },
// {
// "Name": "phone_number",
// "Value": "+16302581871"
// },
// {
// "Name": "email",
// "Value": "admin@chilkatsoft.com"
// }
// ],
// "Enabled": true,
// "UserCreateDate": 1.636407153801E9,
// "UserLastModifiedDate": 1.636407153801E9,
// "UserStatus": "FORCE_CHANGE_PASSWORD",
// "Username": "matt"
// }
// }
NSString *Name = 0;
NSString *Value = 0;
BOOL UserEnabled = [jsonResponse BoolOf: @"User.Enabled"];
NSString *UserUserCreateDate = [jsonResponse StringOf: @"User.UserCreateDate"];
NSString *UserUserLastModifiedDate = [jsonResponse StringOf: @"User.UserLastModifiedDate"];
NSString *UserUserStatus = [jsonResponse StringOf: @"User.UserStatus"];
NSString *UserUsername = [jsonResponse StringOf: @"User.Username"];
int i = 0;
int count_i = [[jsonResponse SizeOfArray: @"User.Attributes"] intValue];
while (i < count_i) {
jsonResponse.I = [NSNumber numberWithInt: i];
Name = [jsonResponse StringOf: @"User.Attributes[i].Name"];
Value = [jsonResponse StringOf: @"User.Attributes[i].Value"];
i = i + 1;
}