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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Request
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Reply
oleobject loo_Files
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Token.LoadFile("jira-token.json")
if li_Success <> 1 then
Write-Debug "Could not read the private token file."
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_Token
return
end if
loo_Request = create oleobject
li_rc = loo_Request.ConnectToNewObject("Chilkat.JsonObject")
loo_Request.UpdateString("grant_type","refresh_token")
loo_Request.UpdateString("client_id","YOUR_CLIENT_ID")
loo_Request.UpdateString("client_secret","YOUR_CLIENT_SECRET")
loo_Request.UpdateString("refresh_token",loo_Token.StringOf("refresh_token"))
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Http.HttpJson("POST","https://auth.atlassian.com/oauth/token",loo_Request,"application/json",loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "No HTTP response was received. Check connectivity and TLS settings."
destroy loo_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
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_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON object in the response."
destroy loo_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
return
end if
if (loo_Reply.JsonTypeOf("access_token") <> 1) OR (loo_Reply.StringOfEquals("access_token","",1) = 1) then
Write-Debug "Missing value: access_token"
destroy loo_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
return
end if
if (loo_Reply.JsonTypeOf("refresh_token") <> 1) OR (loo_Reply.StringOfEquals("refresh_token","",1) = 1) then
Write-Debug "Missing value: refresh_token"
destroy loo_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
return
end if
li_Success = loo_Reply.WriteFile("jira-token.new.json")
if li_Success <> 1 then
Write-Debug "Could not save the rotated token; do not discard the response from memory."
destroy loo_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
return
end if
li_Success = loo_Reply.WriteFile("jira-token.json")
if li_Success <> 1 then
Write-Debug "Could not update the token cache. Recover from jira-token.new.json; it contains the rotated token."
destroy loo_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
return
end if
loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")
li_Success = loo_Files.FileDelete("jira-token.new.json")
if li_Success <> 1 then
Write-Debug "Token cache updated, but the recovery copy could not be removed from the private directory."
destroy loo_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_Files
return
end if
Write-Debug "Access and refresh tokens updated. Expires in seconds: " + string(loo_Reply.IntOf("expires_in"))
destroy loo_Token
destroy loo_Request
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_Files