Sample code for 30+ languages & platforms
AutoIt 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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oOauth = ObjCreate("Chilkat.OAuth2")
$oOauth.AuthorizationEndpoint = "https://auth.atlassian.com/authorize"
$oOauth.TokenEndpoint = "https://auth.atlassian.com/oauth/token"
$oOauth.ClientId = "YOUR_CLIENT_ID"
$oOauth.ClientSecret = "YOUR_CLIENT_SECRET"
$oOauth.LocalHost = "127.0.0.1"
$oOauth.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.
$oOauth.UncommonOptions = "NO_OAUTH2_SCOPE"
$oOauth.UseBasicAuth = False
$oOauth.CodeChallenge = False
$oOauth.Scope = "read:jira-work write:jira-work read:jira-user offline_access"
$bSuccess = $oOauth.AddAuthQueryParam("audience","api.atlassian.com")
If ($bSuccess <> True) Then
    ConsoleWrite("Operation failed." & @CRLF)
    Exit
EndIf

$bSuccess = $oOauth.AddAuthQueryParam("prompt","consent")
If ($bSuccess <> True) Then
    ConsoleWrite("Operation failed." & @CRLF)
    Exit
EndIf

;  Chilkat generates state and validates it when the browser redirects back.
Local $sAuthUrl = $oOauth.StartAuth()
If ($oOauth.LastMethodSuccess <> True) Then
    ConsoleWrite("Could not start the local authorization listener." & @CRLF)
    Exit
EndIf

$bSuccess = $oOauth.LaunchBrowser($sAuthUrl)
If ($bSuccess <> True) Then
    $oOauth.Cancel()
    ConsoleWrite("Could not open the system browser." & @CRLF)
    Exit
EndIf

Local $iWaited = 0
While (($oOauth.AuthFlowState < 3) And ($iWaited < 180000))
    $oOauth.SleepMs 200
    $iWaited = $iWaited + 200
Wend
If ($oOauth.AuthFlowState < 3) Then
    $oOauth.Cancel()
    ConsoleWrite("Authorization timed out." & @CRLF)
    Exit
EndIf

If ($oOauth.AuthFlowState <> 3) Then
    ConsoleWrite("Authorization did not complete. Flow state: " & $oOauth.AuthFlowState & @CRLF)
    Exit
EndIf

$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.Load($oOauth.AccessTokenResponse)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not parse the token response." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("Missing value: access_token" & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("refresh_token") <> 1) Or ($oToken.StringOfEquals("refresh_token","",True) = True)) Then
    ConsoleWrite("Missing value: refresh_token" & @CRLF)
    Exit
EndIf

;  Use a private working directory accessible only to this application/user.
$bSuccess = $oToken.WriteFile("jira-token.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not save the token file." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oHttp.Accept = "application/json"
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpNoBody("GET","https://api.atlassian.com/oauth/token/accessible-resources",$oResp)
If ($oResp.StatusCode = 0) Then
    ConsoleWrite("No HTTP response was received. Check connectivity and TLS settings." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    ConsoleWrite("Request failed. HTTP " & $oResp.StatusCode & @CRLF)
    If ($oResp.StatusCode = 429) Then
        ConsoleWrite("Rate limited. Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
    EndIf

    Exit
EndIf

$oSites = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oSites.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected an array of accessible sites." & @CRLF)
    Exit
EndIf

Local $i = 0
$oSite = ObjCreate("Chilkat.JsonObject")
While ($i < $oSites.Size)
    $oSite = $oSites.ObjectAt($i)
    If ($oSites.LastMethodSuccess <> True) Then
        ConsoleWrite("Invalid site entry." & @CRLF)
        Exit
    EndIf

    ConsoleWrite($oSite.StringOf("name") & "  " & $oSite.StringOf("url") & "  Cloud ID: " & $oSite.StringOf("id") & @CRLF)

    $i = $i + 1
Wend
ConsoleWrite("Tokens saved. Select the intended site's Cloud ID for subsequent API requests." & @CRLF)