Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Jira Cloud: Refresh an OAuth2 Access Token

See more Jira Examples

Refresh a previously authorized Atlassian OAuth2 token without opening a browser. Run initial authorization first. Atlassian rotates refresh tokens: save the newly returned refresh token together with the access token before another refresh. Serialize refreshes for the same grant so concurrent processes do not overwrite each other's tokens.

The example writes a recovery file before updating the existing token cache. Keep both files in a private working directory. If writing the main cache fails, recover from jira-token.new.json. A production token store should provide an atomic replacement or transaction. An HTTP refresh failure leaves the old cache untouched. If authorization is revoked or the refresh token expires, obtain user consent again; do not repeatedly retry an invalid grant. The confidential client secret belongs in private application configuration or a backend, not a publicly distributed executable.

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

Lua
local chilkat = require("chilkat")

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

local token = chilkat.JsonObject.new()
local success = token:load_file("jira-token.json")
if not success then
    print("Could not read the private token file.")
    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

local request = chilkat.JsonObject.new()
request:update_string("grant_type", "refresh_token")
request:update_string("client_id", "YOUR_CLIENT_ID")
request:update_string("client_secret", "YOUR_CLIENT_SECRET")
request:update_string("refresh_token", token:string_of("refresh_token"))
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
success = http:http_json("POST", "https://auth.atlassian.com/oauth/token", request, "application/json", 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

success = reply:load(resp.body_str)
if not success then
    print("Expected a JSON object in the response.")
    return
end

if (reply:json_type_of("access_token") ~= 1) or (reply:string_of_equals("access_token", "", true)) then
    print("Missing value: access_token")
    return
end

if (reply:json_type_of("refresh_token") ~= 1) or (reply:string_of_equals("refresh_token", "", true)) then
    print("Missing value: refresh_token")
    return
end

success = reply:write_file("jira-token.new.json")
if not success then
    print("Could not save the rotated token; do not discard the response from memory.")
    return
end

success = reply:write_file("jira-token.json")
if not success then
    print("Could not update the token cache. Recover from jira-token.new.json; it contains the rotated token.")
    return
end

local files = chilkat.FileAccess.new()
success = files:file_delete("jira-token.new.json")
if not success then
    print("Token cache updated, but the recovery copy could not be removed from the private directory.")
    return
end

print("Access and refresh tokens updated. Expires in seconds: " .. reply:int_of("expires_in"))