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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.OAuth2,
Chilkat.JsonArray,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
oauth: TOAuth2;
authUrl: string;
waited: Integer;
token: TJsonObject;
http: THttp;
resp: THttpResponse;
sites: TJsonArray;
i: Integer;
site: TJsonObject;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
oauth := TOAuth2.Create;
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 := 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 := False;
oauth.CodeChallenge := False;
oauth.Scope := 'read:jira-work write:jira-work read:jira-user offline_access';
success := oauth.AddAuthQueryParam('audience','api.atlassian.com');
if (success <> True) then
begin
WriteLn('Operation failed.');
Exit;
end;
success := oauth.AddAuthQueryParam('prompt','consent');
if (success <> True) then
begin
WriteLn('Operation failed.');
Exit;
end;
// Chilkat generates state and validates it when the browser redirects back.
authUrl := oauth.StartAuth();
if (oauth.LastMethodSuccess <> True) then
begin
WriteLn('Could not start the local authorization listener.');
Exit;
end;
success := oauth.LaunchBrowser(authUrl);
if (success <> True) then
begin
oauth.Cancel();
WriteLn('Could not open the system browser.');
Exit;
end;
waited := 0;
while ((oauth.AuthFlowState < 3) and (waited < 180000)) do
begin
oauth.SleepMs(200);
waited := waited + 200;
end;
if (oauth.AuthFlowState < 3) then
begin
oauth.Cancel();
WriteLn('Authorization timed out.');
Exit;
end;
if (oauth.AuthFlowState <> 3) then
begin
WriteLn('Authorization did not complete. Flow state: ' + oauth.AuthFlowState);
Exit;
end;
token := TJsonObject.Create;
success := token.Load(oauth.AccessTokenResponse);
if (success <> True) then
begin
WriteLn('Could not parse the token response.');
Exit;
end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',True) = True)) then
begin
WriteLn('Missing value: access_token');
Exit;
end;
if ((token.JsonTypeOf('refresh_token') <> 1) or (token.StringOfEquals('refresh_token','',True) = True)) then
begin
WriteLn('Missing value: refresh_token');
Exit;
end;
// Use a private working directory accessible only to this application/user.
success := token.WriteFile('jira-token.json');
if (success <> True) then
begin
WriteLn('Could not save the token file.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := token.StringOf('access_token');
http.Accept := 'application/json';
resp := THttpResponse.Create;
success := http.HttpNoBody('GET','https://api.atlassian.com/oauth/token/accessible-resources',resp);
if (resp.StatusCode = 0) then
begin
WriteLn('No HTTP response was received. Check connectivity and TLS settings.');
Exit;
end;
if ((success <> True) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
WriteLn('Request failed. HTTP ' + resp.StatusCode);
if (resp.StatusCode = 429) then
begin
WriteLn('Rate limited. Retry-After: ' + resp.GetHeaderField('Retry-After'));
end;
Exit;
end;
sites := TJsonArray.Create;
success := sites.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Expected an array of accessible sites.');
Exit;
end;
i := 0;
site := TJsonObject.Create;
while (i < sites.Size) do
begin
site := sites.ObjectAt(i);
if (sites.LastMethodSuccess <> True) then
begin
WriteLn('Invalid site entry.');
Exit;
end;
WriteLn(site.StringOf('name') + ' ' + site.StringOf('url') + ' Cloud ID: ' + site.StringOf('id'));
site.Free;
i := i + 1;
end;
WriteLn('Tokens saved. Select the intended site''s Cloud ID for subsequent API requests.');
oauth.Free;
token.Free;
http.Free;
resp.Free;
sites.Free;
site.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.