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

Visual FoxPro
LOCAL lnSuccess
LOCAL loToken
LOCAL loRequest
LOCAL loHttp
LOCAL loResp
LOCAL loReply
LOCAL loFiles

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("jira-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not read the private token file."
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("refresh_token") <> 1) OR (loToken.StringOfEquals("refresh_token","",1) = 1)) THEN
    ? "Missing value: refresh_token"
    RELEASE loToken
    CANCEL
ENDIF

loRequest = CreateObject('Chilkat.JsonObject')
loRequest.UpdateString("grant_type","refresh_token")
loRequest.UpdateString("client_id","YOUR_CLIENT_ID")
loRequest.UpdateString("client_secret","YOUR_CLIENT_SECRET")
loRequest.UpdateString("refresh_token",loToken.StringOf("refresh_token"))
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loResp = CreateObject('Chilkat.HttpResponse')
loReply = CreateObject('Chilkat.JsonObject')
lnSuccess = loHttp.HttpJson("POST","https://auth.atlassian.com/oauth/token",loRequest,"application/json",loResp)
IF (loResp.StatusCode = 0) THEN
    ? "No HTTP response was received. Check connectivity and TLS settings."
    RELEASE loToken
    RELEASE loRequest
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF ((lnSuccess <> 1) OR (loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    ? "Request failed. HTTP " + STR(loResp.StatusCode)
    IF (loResp.StatusCode = 429) THEN
        ? "Rate limited. Retry-After: " + loResp.GetHeaderField("Retry-After")
    ENDIF

    RELEASE loToken
    RELEASE loRequest
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a JSON object in the response."
    RELEASE loToken
    RELEASE loRequest
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF ((loReply.JsonTypeOf("access_token") <> 1) OR (loReply.StringOfEquals("access_token","",1) = 1)) THEN
    ? "Missing value: access_token"
    RELEASE loToken
    RELEASE loRequest
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF ((loReply.JsonTypeOf("refresh_token") <> 1) OR (loReply.StringOfEquals("refresh_token","",1) = 1)) THEN
    ? "Missing value: refresh_token"
    RELEASE loToken
    RELEASE loRequest
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

lnSuccess = loReply.WriteFile("jira-token.new.json")
IF (lnSuccess <> 1) THEN
    ? "Could not save the rotated token; do not discard the response from memory."
    RELEASE loToken
    RELEASE loRequest
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

lnSuccess = loReply.WriteFile("jira-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not update the token cache. Recover from jira-token.new.json; it contains the rotated token."
    RELEASE loToken
    RELEASE loRequest
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

loFiles = CreateObject('Chilkat.FileAccess')
lnSuccess = loFiles.FileDelete("jira-token.new.json")
IF (lnSuccess <> 1) THEN
    ? "Token cache updated, but the recovery copy could not be removed from the private directory."
    RELEASE loToken
    RELEASE loRequest
    RELEASE loHttp
    RELEASE loResp
    RELEASE loReply
    RELEASE loFiles
    CANCEL
ENDIF

? "Access and refresh tokens updated. Expires in seconds: " + STR(loReply.IntOf("expires_in"))

RELEASE loToken
RELEASE loRequest
RELEASE loHttp
RELEASE loResp
RELEASE loReply
RELEASE loFiles