Objective-C
Objective-C
Okta: Use the Resource Owner Password Flow
See more Okta OAuth/OIDC Examples
Demonstrates how to get an access token using the Resource Owner Password Flow.Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoHttpRequest.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>
BOOL success = NO;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoHttp *http = [[CkoHttp alloc] init];
// Implements the following CURL command:
// curl --request POST \
// --url https://{yourOktaDomain}/oauth2/default/v1/token \
// --header 'accept: application/json' \
// --user "client_id:client_secret" \
// --header 'content-type: application/x-www-form-urlencoded' \
// --data 'grant_type=password&username=myUserEmail&password=myPassword&scope=openid'
http.Login = @"client_id";
http.Password = @"client_secret";
CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
req.HttpVerb = @"POST";
req.Path = @"/oauth2/default/v1/token";
req.ContentType = @"application/x-www-form-urlencoded";
[req AddParam: @"grant_type" value: @"password"];
[req AddParam: @"username" value: @"myUserEmail"];
[req AddParam: @"password" value: @"myPassword"];
[req AddParam: @"scope" value: @"openid"];
[req AddHeader: @"accept" value: @"application/json"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpReq: @"https://{yourOktaDomain}/oauth2/default/v1/token" request: req response: resp];
if (success == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
[resp GetBodySb: sbResponseBody];
CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp LoadSb: sbResponseBody];
jResp.EmitCompact = NO;
NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[jResp Emit]);
int respStatusCode = [resp.StatusCode intValue];
NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
if (respStatusCode >= 400) {
NSLog(@"%@",@"Response Header:");
NSLog(@"%@",resp.Header);
NSLog(@"%@",@"Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "access_token": "eyJraWQiOi ... jmiHD7wY9_gQ",
// "token_type": "Bearer",
// "expires_in": 3600,
// "scope": "openid",
// "id_token": "eyJraWQiOiJ ... W7KkWiPJnUSMoGw"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
NSString *access_token = [jResp StringOf: @"access_token"];
NSString *token_type = [jResp StringOf: @"token_type"];
int expires_in = [[jResp IntOf: @"expires_in"] intValue];
NSString *scope = [jResp StringOf: @"scope"];
NSString *id_token = [jResp StringOf: @"id_token"];