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 Delphi DLL Downloads
var
success: Boolean;
oauth: HCkOAuth2;
authUrl: PWideChar;
waited: Integer;
token: HCkJsonObject;
http: HCkHttp;
resp: HCkHttpResponse;
sites: HCkJsonArray;
i: Integer;
site: HCkJsonObject;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
oauth := CkOAuth2_Create();
CkOAuth2_putAuthorizationEndpoint(oauth,'https://auth.atlassian.com/authorize');
CkOAuth2_putTokenEndpoint(oauth,'https://auth.atlassian.com/oauth/token');
CkOAuth2_putClientId(oauth,'YOUR_CLIENT_ID');
CkOAuth2_putClientSecret(oauth,'YOUR_CLIENT_SECRET');
CkOAuth2_putLocalHost(oauth,'127.0.0.1');
CkOAuth2_putListenPort(oauth,3017);
// Register exactly http://127.0.0.1:3017/ in the developer console.
// Keep scope on the authorization request, not the authorization-code exchange.
CkOAuth2_putUncommonOptions(oauth,'NO_OAUTH2_SCOPE');
CkOAuth2_putUseBasicAuth(oauth,False);
CkOAuth2_putCodeChallenge(oauth,False);
CkOAuth2_putScope(oauth,'read:jira-work write:jira-work read:jira-user offline_access');
success := CkOAuth2_AddAuthQueryParam(oauth,'audience','api.atlassian.com');
if (success <> True) then
begin
Memo1.Lines.Add('Operation failed.');
Exit;
end;
success := CkOAuth2_AddAuthQueryParam(oauth,'prompt','consent');
if (success <> True) then
begin
Memo1.Lines.Add('Operation failed.');
Exit;
end;
// Chilkat generates state and validates it when the browser redirects back.
authUrl := CkOAuth2__startAuth(oauth);
if (CkOAuth2_getLastMethodSuccess(oauth) <> True) then
begin
Memo1.Lines.Add('Could not start the local authorization listener.');
Exit;
end;
success := CkOAuth2_LaunchBrowser(oauth,authUrl);
if (success <> True) then
begin
CkOAuth2_Cancel(oauth);
Memo1.Lines.Add('Could not open the system browser.');
Exit;
end;
waited := 0;
while ((CkOAuth2_getAuthFlowState(oauth) < 3) and (waited < 180000)) do
begin
CkOAuth2_SleepMs(oauth,200);
waited := waited + 200;
end;
if (CkOAuth2_getAuthFlowState(oauth) < 3) then
begin
CkOAuth2_Cancel(oauth);
Memo1.Lines.Add('Authorization timed out.');
Exit;
end;
if (CkOAuth2_getAuthFlowState(oauth) <> 3) then
begin
Memo1.Lines.Add('Authorization did not complete. Flow state: ' + IntToStr(CkOAuth2_getAuthFlowState(oauth)));
Exit;
end;
token := CkJsonObject_Create();
success := CkJsonObject_Load(token,CkOAuth2__accessTokenResponse(oauth));
if (success <> True) then
begin
Memo1.Lines.Add('Could not parse the token response.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (CkJsonObject_StringOfEquals(token,'access_token','',True) = True)) then
begin
Memo1.Lines.Add('Missing value: access_token');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(token,'refresh_token') <> 1) or (CkJsonObject_StringOfEquals(token,'refresh_token','',True) = True)) then
begin
Memo1.Lines.Add('Missing value: refresh_token');
Exit;
end;
// Use a private working directory accessible only to this application/user.
success := CkJsonObject_WriteFile(token,'jira-token.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not save the token file.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
CkHttp_putAccept(http,'application/json');
resp := CkHttpResponse_Create();
success := CkHttp_HttpNoBody(http,'GET','https://api.atlassian.com/oauth/token/accessible-resources',resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add('No HTTP response was received. Check connectivity and TLS settings.');
Exit;
end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
begin
Memo1.Lines.Add('Request failed. HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
if (CkHttpResponse_getStatusCode(resp) = 429) then
begin
Memo1.Lines.Add('Rate limited. Retry-After: ' + CkHttpResponse__getHeaderField(resp,'Retry-After'));
end;
Exit;
end;
sites := CkJsonArray_Create();
success := CkJsonArray_Load(sites,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Expected an array of accessible sites.');
Exit;
end;
i := 0;
site := CkJsonObject_Create();
while (i < CkJsonArray_getSize(sites)) do
begin
site := CkJsonArray_ObjectAt(sites,i);
if (CkJsonArray_getLastMethodSuccess(sites) <> True) then
begin
Memo1.Lines.Add('Invalid site entry.');
Exit;
end;
Memo1.Lines.Add(CkJsonObject__stringOf(site,'name') + ' ' + CkJsonObject__stringOf(site,'url') + ' Cloud ID: ' + CkJsonObject__stringOf(site,'id'));
CkJsonObject_Dispose(site);
i := i + 1;
end;
Memo1.Lines.Add('Tokens saved. Select the intended site''s Cloud ID for subsequent API requests.');
CkOAuth2_Dispose(oauth);
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonArray_Dispose(sites);
CkJsonObject_Dispose(site);