Jira Cloud: Refresh an OAuth2 Access Token
See more Jira Examples
Refresh a previously authorized Atlassian OAuth2 token without opening a browser. Run initial authorization first. Atlassian rotates refresh tokens: save the newly returned refresh token together with the access token before another refresh. Serialize refreshes for the same grant so concurrent processes do not overwrite each other's tokens.
The example writes a recovery file before updating the existing token cache. Keep both files in a private working directory. If writing the main cache fails, recover from jira-token.new.json. A production token store should provide an atomic replacement or transaction. An HTTP refresh failure leaves the old cache untouched. If authorization is revoked or the refresh token expires, obtain user consent again; do not repeatedly retry an invalid grant. The confidential client secret belongs in private application configuration or a backend, not a publicly distributed executable.
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
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoFileAccess.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token LoadFile: @"jira-token.json"];
if (success != YES) {
NSLog(@"%@",@"Could not read the private token file.");
return;
}
if (([[token JsonTypeOf: @"refresh_token"] intValue] != 1) || ([token StringOfEquals: @"refresh_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Missing value: refresh_token");
return;
}
CkoJsonObject *request = [[CkoJsonObject alloc] init];
[request UpdateString: @"grant_type" value: @"refresh_token"];
[request UpdateString: @"client_id" value: @"YOUR_CLIENT_ID"];
[request UpdateString: @"client_secret" value: @"YOUR_CLIENT_SECRET"];
[request UpdateString: @"refresh_token" value: [token StringOf: @"refresh_token"]];
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
success = [http HttpJson: @"POST" url: @"https://auth.atlassian.com/oauth/token" json: request contentType: @"application/json" 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;
}
success = [reply Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON object in the response.");
return;
}
if (([[reply JsonTypeOf: @"access_token"] intValue] != 1) || ([reply StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Missing value: access_token");
return;
}
if (([[reply JsonTypeOf: @"refresh_token"] intValue] != 1) || ([reply StringOfEquals: @"refresh_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Missing value: refresh_token");
return;
}
success = [reply WriteFile: @"jira-token.new.json"];
if (success != YES) {
NSLog(@"%@",@"Could not save the rotated token; do not discard the response from memory.");
return;
}
success = [reply WriteFile: @"jira-token.json"];
if (success != YES) {
NSLog(@"%@",@"Could not update the token cache. Recover from jira-token.new.json; it contains the rotated token.");
return;
}
CkoFileAccess *files = [[CkoFileAccess alloc] init];
success = [files FileDelete: @"jira-token.new.json"];
if (success != YES) {
NSLog(@"%@",@"Token cache updated, but the recovery copy could not be removed from the private directory.");
return;
}
NSLog(@"%@%d",@"Access and refresh tokens updated. Expires in seconds: ",[[reply IntOf: @"expires_in"] intValue]);