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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local oauth = chilkat.OAuth2.new()
oauth.authorization_endpoint = "https://auth.atlassian.com/authorize"
oauth.token_endpoint = "https://auth.atlassian.com/oauth/token"
oauth.client_id = "YOUR_CLIENT_ID"
oauth.client_secret = "YOUR_CLIENT_SECRET"
oauth.local_host = "127.0.0.1"
oauth.listen_port = 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.uncommon_options = "NO_OAUTH2_SCOPE"
oauth.use_basic_auth = false
oauth.code_challenge = false
oauth.scope = "read:jira-work write:jira-work read:jira-user offline_access"
local success = oauth:add_auth_query_param("audience", "api.atlassian.com")
if not success then
print("Operation failed.")
return
end
success = oauth:add_auth_query_param("prompt", "consent")
if not success then
print("Operation failed.")
return
end
-- Chilkat generates state and validates it when the browser redirects back.
local authUrl = oauth:start_auth()
if not oauth.last_method_success then
print("Could not start the local authorization listener.")
return
end
success = oauth:launch_browser(authUrl)
if not success then
oauth:cancel()
print("Could not open the system browser.")
return
end
local waited = 0
while (oauth.auth_flow_state < 3) and (waited < 180000) do
oauth:sleep_ms(200)
waited = waited + 200
end
if oauth.auth_flow_state < 3 then
oauth:cancel()
print("Authorization timed out.")
return
end
if oauth.auth_flow_state ~= 3 then
print("Authorization did not complete. Flow state: " .. oauth.auth_flow_state)
return
end
local token = chilkat.JsonObject.new()
success = token:load(oauth.access_token_response)
if not success then
print("Could not parse the token response.")
return
end
if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
print("Missing value: access_token")
return
end
if (token:json_type_of("refresh_token") ~= 1) or (token:string_of_equals("refresh_token", "", true)) then
print("Missing value: refresh_token")
return
end
-- Use a private working directory accessible only to this application/user.
success = token:write_file("jira-token.json")
if not success then
print("Could not save the token file.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = token:string_of("access_token")
http.accept = "application/json"
local resp = chilkat.HttpResponse.new()
success = http:http_no_body("GET", "https://api.atlassian.com/oauth/token/accessible-resources", resp)
if resp.status_code == 0 then
print("No HTTP response was received. Check connectivity and TLS settings.")
return
end
if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
print("Request failed. HTTP " .. resp.status_code)
if resp.status_code == 429 then
print("Rate limited. Retry-After: " .. resp:get_header_field("Retry-After"))
end
return
end
local sites = chilkat.JsonArray.new()
success = sites:load(resp.body_str)
if not success then
print("Expected an array of accessible sites.")
return
end
local i = 0
local site = chilkat.JsonObject.new()
while i < sites.size do
site = sites:object_at(i)
if not sites.last_method_success then
print("Invalid site entry.")
return
end
print(site:string_of("name") .. " " .. site:string_of("url") .. " Cloud ID: " .. site:string_of("id"))
i = i + 1
end
print("Tokens saved. Select the intended site's Cloud ID for subsequent API requests.")