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

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set oauth [new_CkOAuth2]

CkOAuth2_put_AuthorizationEndpoint $oauth "https://auth.atlassian.com/authorize"
CkOAuth2_put_TokenEndpoint $oauth "https://auth.atlassian.com/oauth/token"
CkOAuth2_put_ClientId $oauth "YOUR_CLIENT_ID"
CkOAuth2_put_ClientSecret $oauth "YOUR_CLIENT_SECRET"
CkOAuth2_put_LocalHost $oauth "127.0.0.1"
CkOAuth2_put_ListenPort $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_put_UncommonOptions $oauth "NO_OAUTH2_SCOPE"
CkOAuth2_put_UseBasicAuth $oauth 0
CkOAuth2_put_CodeChallenge $oauth 0
CkOAuth2_put_Scope $oauth "read:jira-work write:jira-work read:jira-user offline_access"
set success [CkOAuth2_AddAuthQueryParam $oauth "audience" "api.atlassian.com"]
if {$success != 1} then {
    puts "Operation failed."
    delete_CkOAuth2 $oauth
    exit
}

set success [CkOAuth2_AddAuthQueryParam $oauth "prompt" "consent"]
if {$success != 1} then {
    puts "Operation failed."
    delete_CkOAuth2 $oauth
    exit
}

#  Chilkat generates state and validates it when the browser redirects back.
set authUrl [CkOAuth2_startAuth $oauth]
if {[CkOAuth2_get_LastMethodSuccess $oauth] != 1} then {
    puts "Could not start the local authorization listener."
    delete_CkOAuth2 $oauth
    exit
}

set success [CkOAuth2_LaunchBrowser $oauth $authUrl]
if {$success != 1} then {
    CkOAuth2_Cancel $oauth
    puts "Could not open the system browser."
    delete_CkOAuth2 $oauth
    exit
}

set waited 0
while {expr [[CkOAuth2_get_AuthFlowState $oauth] < 3]  &&  [$waited < 180000]} {
    CkOAuth2_SleepMs $oauth 200
    set waited [expr $waited + 200]
}
if {[CkOAuth2_get_AuthFlowState $oauth] < 3} then {
    CkOAuth2_Cancel $oauth
    puts "Authorization timed out."
    delete_CkOAuth2 $oauth
    exit
}

if {[CkOAuth2_get_AuthFlowState $oauth] != 3} then {
    puts "Authorization did not complete. Flow state: [CkOAuth2_get_AuthFlowState $oauth]"
    delete_CkOAuth2 $oauth
    exit
}

set token [new_CkJsonObject]

set success [CkJsonObject_Load $token [CkOAuth2_accessTokenResponse $oauth]]
if {$success != 1} then {
    puts "Could not parse the token response."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
    puts "Missing value: access_token"
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "refresh_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "refresh_token" "" 1] == 1]} then {
    puts "Missing value: refresh_token"
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

#  Use a private working directory accessible only to this application/user.
set success [CkJsonObject_WriteFile $token "jira-token.json"]
if {$success != 1} then {
    puts "Could not save the token file."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_AuthToken $http [CkJsonObject_stringOf $token "access_token"]
CkHttp_put_Accept $http "application/json"
set resp [new_CkHttpResponse]

set success [CkHttp_HttpNoBody $http "GET" "https://api.atlassian.com/oauth/token/accessible-resources" $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
    puts "No HTTP response was received. Check connectivity and TLS settings."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    exit
}

if {expr [$success != 1]  ||  [expr [[CkHttpResponse_get_StatusCode $resp] < 200]  ||  [[CkHttpResponse_get_StatusCode $resp] >= 300]]} then {
    puts "Request failed. HTTP [CkHttpResponse_get_StatusCode $resp]"
    if {[CkHttpResponse_get_StatusCode $resp] == 429} then {
        puts "Rate limited. Retry-After: [CkHttpResponse_getHeaderField $resp Retry-After]"
    }

    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    exit
}

set sites [new_CkJsonArray]

set success [CkJsonArray_Load $sites [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
    puts "Expected an array of accessible sites."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonArray $sites
    exit
}

set i 0
set site [new_CkJsonObject]

while {$i < [CkJsonArray_get_Size $sites]} {
    set site [CkJsonArray_ObjectAt $sites $i]
    if {[CkJsonArray_get_LastMethodSuccess $sites] != 1} then {
        puts "Invalid site entry."
        delete_CkOAuth2 $oauth
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonArray $sites
        delete_CkJsonObject $site
        exit
    }

    puts [CkJsonObject_stringOf $site name]  [CkJsonObject_stringOf $site url]  Cloud ID: [CkJsonObject_stringOf $site id]
    delete_CkJsonObject $site

    set i [expr $i + 1]
}
puts "Tokens saved. Select the intended site's Cloud ID for subsequent API requests."

delete_CkOAuth2 $oauth
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonArray $sites
delete_CkJsonObject $site