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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Oauth
string ls_AuthUrl
long li_Waited
oleobject loo_Token
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Sites
long i
oleobject loo_Site

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Oauth = create oleobject
li_rc = loo_Oauth.ConnectToNewObject("Chilkat.OAuth2")
if li_rc < 0 then
    destroy loo_Oauth
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Oauth.AuthorizationEndpoint = "https://auth.atlassian.com/authorize"
loo_Oauth.TokenEndpoint = "https://auth.atlassian.com/oauth/token"
loo_Oauth.ClientId = "YOUR_CLIENT_ID"
loo_Oauth.ClientSecret = "YOUR_CLIENT_SECRET"
loo_Oauth.LocalHost = "127.0.0.1"
loo_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.
loo_Oauth.UncommonOptions = "NO_OAUTH2_SCOPE"
loo_Oauth.UseBasicAuth = 0
loo_Oauth.CodeChallenge = 0
loo_Oauth.Scope = "read:jira-work write:jira-work read:jira-user offline_access"
li_Success = loo_Oauth.AddAuthQueryParam("audience","api.atlassian.com")
if li_Success <> 1 then
    Write-Debug "Operation failed."
    destroy loo_Oauth
    return
end if

li_Success = loo_Oauth.AddAuthQueryParam("prompt","consent")
if li_Success <> 1 then
    Write-Debug "Operation failed."
    destroy loo_Oauth
    return
end if

//  Chilkat generates state and validates it when the browser redirects back.
ls_AuthUrl = loo_Oauth.StartAuth()
if loo_Oauth.LastMethodSuccess <> 1 then
    Write-Debug "Could not start the local authorization listener."
    destroy loo_Oauth
    return
end if

li_Success = loo_Oauth.LaunchBrowser(ls_AuthUrl)
if li_Success <> 1 then
    loo_Oauth.Cancel()
    Write-Debug "Could not open the system browser."
    destroy loo_Oauth
    return
end if

li_Waited = 0
do while ((loo_Oauth.AuthFlowState < 3) AND (li_Waited < 180000))
    loo_Oauth.SleepMs(200)
    li_Waited = li_Waited + 200
loop
if loo_Oauth.AuthFlowState < 3 then
    loo_Oauth.Cancel()
    Write-Debug "Authorization timed out."
    destroy loo_Oauth
    return
end if

if loo_Oauth.AuthFlowState <> 3 then
    Write-Debug "Authorization did not complete. Flow state: " + string(loo_Oauth.AuthFlowState)
    destroy loo_Oauth
    return
end if

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Token.Load(loo_Oauth.AccessTokenResponse)
if li_Success <> 1 then
    Write-Debug "Could not parse the token response."
    destroy loo_Oauth
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "Missing value: access_token"
    destroy loo_Oauth
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("refresh_token") <> 1) OR (loo_Token.StringOfEquals("refresh_token","",1) = 1) then
    Write-Debug "Missing value: refresh_token"
    destroy loo_Oauth
    destroy loo_Token
    return
end if

//  Use a private working directory accessible only to this application/user.
li_Success = loo_Token.WriteFile("jira-token.json")
if li_Success <> 1 then
    Write-Debug "Could not save the token file."
    destroy loo_Oauth
    destroy loo_Token
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Token.StringOf("access_token")
loo_Http.Accept = "application/json"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpNoBody("GET","https://api.atlassian.com/oauth/token/accessible-resources",loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug "No HTTP response was received. Check connectivity and TLS settings."
    destroy loo_Oauth
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    return
end if

if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    Write-Debug "Request failed. HTTP " + string(loo_Resp.StatusCode)
    if loo_Resp.StatusCode = 429 then
        Write-Debug "Rate limited. Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
    end if

    destroy loo_Oauth
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    return
end if

loo_Sites = create oleobject
li_rc = loo_Sites.ConnectToNewObject("Chilkat.JsonArray")

li_Success = loo_Sites.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Expected an array of accessible sites."
    destroy loo_Oauth
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Sites
    return
end if

i = 0
loo_Site = create oleobject
li_rc = loo_Site.ConnectToNewObject("Chilkat.JsonObject")

do while (i < loo_Sites.Size)
    loo_Site = loo_Sites.ObjectAt(i)
    if loo_Sites.LastMethodSuccess <> 1 then
        Write-Debug "Invalid site entry."
        destroy loo_Oauth
        destroy loo_Token
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Sites
        destroy loo_Site
        return
    end if

    Write-Debug loo_Site.StringOf("name") + "  " + loo_Site.StringOf("url") + "  Cloud ID: " + loo_Site.StringOf("id")
    destroy loo_Site
    i = i + 1
loop
Write-Debug "Tokens saved. Select the intended site's Cloud ID for subsequent API requests."


destroy loo_Oauth
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Sites
destroy loo_Site