Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Jira Cloud: Obtain an OAuth2 Access Token

See more Jira Examples

Authorize an integration, save the access and refresh tokens, and list the sites available to the token. Register an OAuth2 (3LO) app in the Atlassian developer console, enable the Jira API scopes, and register the exact callback http://127.0.0.1:3017/. Run this interactive example on the same computer as the browser.

This demonstrates Atlassian's documented confidential-client flow for a privately operated integration. The client secret must remain confidential; do not embed it in an application distributed to users. A distributed client should use an application backend to perform the confidential token exchange. This example does not implement a public-client PKCE flow.

The core scopes shown cover the platform examples. Jira Software and Service Management have additional endpoint-specific scopes: add the needed scopes in the developer console and the authorization request, then obtain consent again. offline_access requests a rotating refresh token. Store the token file in a private directory, never print its contents, and select the intended site explicitly rather than assuming the first returned site is correct.

Targets Jira Cloud. Jira Data Center has a separate REST API and may require different paths, authentication, or payloads.

Atlassian REST API documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoOAuth2.h>
#import <NSString.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoJsonArray.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

CkoOAuth2 *oauth = [[CkoOAuth2 alloc] init];
oauth.AuthorizationEndpoint = @"https://auth.atlassian.com/authorize";
oauth.TokenEndpoint = @"https://auth.atlassian.com/oauth/token";
oauth.ClientId = @"YOUR_CLIENT_ID";
oauth.ClientSecret = @"YOUR_CLIENT_SECRET";
oauth.LocalHost = @"127.0.0.1";
oauth.ListenPort = [NSNumber numberWithInt:3017];
//  Register exactly http://127.0.0.1:3017/ in the developer console.
//  Keep scope on the authorization request, not the authorization-code exchange.
oauth.UncommonOptions = @"NO_OAUTH2_SCOPE";
oauth.UseBasicAuth = NO;
oauth.CodeChallenge = NO;
oauth.Scope = @"read:jira-work write:jira-work read:jira-user offline_access";
success = [oauth AddAuthQueryParam: @"audience" value: @"api.atlassian.com"];
if (success != YES) {
    NSLog(@"%@",@"Operation failed.");
    return;
}

success = [oauth AddAuthQueryParam: @"prompt" value: @"consent"];
if (success != YES) {
    NSLog(@"%@",@"Operation failed.");
    return;
}

//  Chilkat generates state and validates it when the browser redirects back.
NSString *authUrl = [oauth StartAuth];
if (oauth.LastMethodSuccess != YES) {
    NSLog(@"%@",@"Could not start the local authorization listener.");
    return;
}

success = [oauth LaunchBrowser: authUrl];
if (success != YES) {
    [oauth Cancel];
    NSLog(@"%@",@"Could not open the system browser.");
    return;
}

int waited = 0;
while ((([oauth.AuthFlowState intValue] < 3) && (waited < 180000))) {
    [oauth SleepMs: [NSNumber numberWithInt: 200]];
    waited = waited + 200;
}

if ([oauth.AuthFlowState intValue] < 3) {
    [oauth Cancel];
    NSLog(@"%@",@"Authorization timed out.");
    return;
}

if ([oauth.AuthFlowState intValue] != 3) {
    NSLog(@"%@%d",@"Authorization did not complete. Flow state: ",[oauth.AuthFlowState intValue]);
    return;
}

CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token Load: oauth.AccessTokenResponse];
if (success != YES) {
    NSLog(@"%@",@"Could not parse the token response.");
    return;
}

if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Missing value: access_token");
    return;
}

if (([[token JsonTypeOf: @"refresh_token"] intValue] != 1) || ([token StringOfEquals: @"refresh_token" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Missing value: refresh_token");
    return;
}

//  Use a private working directory accessible only to this application/user.
success = [token WriteFile: @"jira-token.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not save the token file.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [token StringOf: @"access_token"];
http.Accept = @"application/json";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpNoBody: @"GET" url: @"https://api.atlassian.com/oauth/token/accessible-resources" response: resp];
if ([resp.StatusCode intValue] == 0) {
    NSLog(@"%@",@"No HTTP response was received. Check connectivity and TLS settings.");
    return;
}

if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
    NSLog(@"%@%d",@"Request failed. HTTP ",[resp.StatusCode intValue]);
    if ([resp.StatusCode intValue] == 429) {
        NSLog(@"%@%@",@"Rate limited. Retry-After: ",[resp GetHeaderField: @"Retry-After"]);
    }

    return;
}

CkoJsonArray *sites = [[CkoJsonArray alloc] init];
success = [sites Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Expected an array of accessible sites.");
    return;
}

int i = 0;
CkoJsonObject *site = [[CkoJsonObject alloc] init];
while ((i < [sites.Size intValue])) {
    site = [sites ObjectAt: [NSNumber numberWithInt: i]];
    if (sites.LastMethodSuccess != YES) {
        NSLog(@"%@",@"Invalid site entry.");
        return;
    }

    NSLog(@"%@%@%@%@%@",[site StringOf: @"name"],@"  ",[site StringOf: @"url"],@"  Cloud ID: ",[site StringOf: @"id"]);

    i = i + 1;
}

NSLog(@"%@",@"Tokens saved. Select the intended site's Cloud ID for subsequent API requests.");