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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoOauth
String sAuthUrl
Integer iWaited
Handle hoToken
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoSites
Integer i
Handle hoSite
String sTemp1
String sTemp2
String sTemp3
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatOAuth2)) To hoOauth
If (Not(IsComObjectCreated(hoOauth))) Begin
Send CreateComObject of hoOauth
End
Set ComAuthorizationEndpoint Of hoOauth To "https://auth.atlassian.com/authorize"
Set ComTokenEndpoint Of hoOauth To "https://auth.atlassian.com/oauth/token"
Set ComClientId Of hoOauth To "YOUR_CLIENT_ID"
Set ComClientSecret Of hoOauth To "YOUR_CLIENT_SECRET"
Set ComLocalHost Of hoOauth To "127.0.0.1"
Set ComListenPort Of hoOauth To 3017
// Register exactly http://127.0.0.1:3017/ in the developer console.
// Keep scope on the authorization request, not the authorization-code exchange.
Set ComUncommonOptions Of hoOauth To "NO_OAUTH2_SCOPE"
Set ComUseBasicAuth Of hoOauth To False
Set ComCodeChallenge Of hoOauth To False
Set ComScope Of hoOauth To "read:jira-work write:jira-work read:jira-user offline_access"
Get ComAddAuthQueryParam Of hoOauth "audience" "api.atlassian.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Operation failed."
Procedure_Return
End
Get ComAddAuthQueryParam Of hoOauth "prompt" "consent" To iSuccess
If (iSuccess <> True) Begin
Showln "Operation failed."
Procedure_Return
End
// Chilkat generates state and validates it when the browser redirects back.
Get ComStartAuth Of hoOauth To sAuthUrl
Get ComLastMethodSuccess Of hoOauth To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not start the local authorization listener."
Procedure_Return
End
Get ComLaunchBrowser Of hoOauth sAuthUrl To iSuccess
If (iSuccess <> True) Begin
Get ComCancel Of hoOauth To iSuccess
Showln "Could not open the system browser."
Procedure_Return
End
Move 0 To iWaited
While (((ComAuthFlowState(hoOauth)) < 3) And (iWaited < 180000))
Send ComSleepMs To hoOauth 200
Move (iWaited + 200) To iWaited
Loop
Get ComAuthFlowState Of hoOauth To iTemp1
If (iTemp1 < 3) Begin
Get ComCancel Of hoOauth To iSuccess
Showln "Authorization timed out."
Procedure_Return
End
Get ComAuthFlowState Of hoOauth To iTemp1
If (iTemp1 <> 3) Begin
Get ComAuthFlowState Of hoOauth To iTemp1
Showln "Authorization did not complete. Flow state: " iTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComAccessTokenResponse Of hoOauth To sTemp1
Get ComLoad Of hoToken sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not parse the token response."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Missing value: access_token"
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "refresh_token" To iTemp1
Get ComStringOfEquals Of hoToken "refresh_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Missing value: refresh_token"
Procedure_Return
End
// Use a private working directory accessible only to this application/user.
Get ComWriteFile Of hoToken "jira-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not save the token file."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Get ComStringOf Of hoToken "access_token" To sTemp1
Set ComAuthToken Of hoHttp To sTemp1
Set ComAccept Of hoHttp To "application/json"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" "https://api.atlassian.com/oauth/token/accessible-resources" vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No HTTP response was received. Check connectivity and TLS settings."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Request failed. HTTP " iTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Rate limited. Retry-After: " sTemp1
End
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonArray)) To hoSites
If (Not(IsComObjectCreated(hoSites))) Begin
Send CreateComObject of hoSites
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoSites sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected an array of accessible sites."
Procedure_Return
End
Move 0 To i
Get Create (RefClass(cComChilkatJsonObject)) To hoSite
If (Not(IsComObjectCreated(hoSite))) Begin
Send CreateComObject of hoSite
End
While (i < (ComSize(hoSites)))
Get ComObjectAt Of hoSites i To vSite
If (IsComObject(vSite)) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoSite
Set pvComObject Of hoSite To vSite
End
Get ComLastMethodSuccess Of hoSites To bTemp1
If (bTemp1 <> True) Begin
Showln "Invalid site entry."
Procedure_Return
End
Get ComStringOf Of hoSite "name" To sTemp1
Get ComStringOf Of hoSite "url" To sTemp2
Get ComStringOf Of hoSite "id" To sTemp3
Showln sTemp1 " " sTemp2 " Cloud ID: " sTemp3
Send Destroy of hoSite
Move (i + 1) To i
Loop
Showln "Tokens saved. Select the intended site's Cloud ID for subsequent API requests."
End_Procedure