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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loOauth
LOCAL lcAuthUrl
LOCAL lnWaited
LOCAL loToken
LOCAL loHttp
LOCAL loResp
LOCAL loSites
LOCAL i
LOCAL loSite
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
loOauth = CreateObject('Chilkat.OAuth2')
loOauth.AuthorizationEndpoint = "https://auth.atlassian.com/authorize"
loOauth.TokenEndpoint = "https://auth.atlassian.com/oauth/token"
loOauth.ClientId = "YOUR_CLIENT_ID"
loOauth.ClientSecret = "YOUR_CLIENT_SECRET"
loOauth.LocalHost = "127.0.0.1"
loOauth.ListenPort = 3017
* Register exactly http://127.0.0.1:3017/ in the developer console.
* Keep scope on the authorization request, not the authorization-code exchange.
loOauth.UncommonOptions = "NO_OAUTH2_SCOPE"
loOauth.UseBasicAuth = 0
loOauth.CodeChallenge = 0
loOauth.Scope = "read:jira-work write:jira-work read:jira-user offline_access"
lnSuccess = loOauth.AddAuthQueryParam("audience","api.atlassian.com")
IF (lnSuccess <> 1) THEN
? "Operation failed."
RELEASE loOauth
CANCEL
ENDIF
lnSuccess = loOauth.AddAuthQueryParam("prompt","consent")
IF (lnSuccess <> 1) THEN
? "Operation failed."
RELEASE loOauth
CANCEL
ENDIF
* Chilkat generates state and validates it when the browser redirects back.
lcAuthUrl = loOauth.StartAuth()
IF (loOauth.LastMethodSuccess <> 1) THEN
? "Could not start the local authorization listener."
RELEASE loOauth
CANCEL
ENDIF
lnSuccess = loOauth.LaunchBrowser(lcAuthUrl)
IF (lnSuccess <> 1) THEN
loOauth.Cancel()
? "Could not open the system browser."
RELEASE loOauth
CANCEL
ENDIF
lnWaited = 0
DO WHILE ((loOauth.AuthFlowState < 3) AND (lnWaited < 180000))
loOauth.SleepMs(200)
lnWaited = lnWaited + 200
ENDDO
IF (loOauth.AuthFlowState < 3) THEN
loOauth.Cancel()
? "Authorization timed out."
RELEASE loOauth
CANCEL
ENDIF
IF (loOauth.AuthFlowState <> 3) THEN
? "Authorization did not complete. Flow state: " + STR(loOauth.AuthFlowState)
RELEASE loOauth
CANCEL
ENDIF
loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.Load(loOauth.AccessTokenResponse)
IF (lnSuccess <> 1) THEN
? "Could not parse the token response."
RELEASE loOauth
RELEASE loToken
CANCEL
ENDIF
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
? "Missing value: access_token"
RELEASE loOauth
RELEASE loToken
CANCEL
ENDIF
IF ((loToken.JsonTypeOf("refresh_token") <> 1) OR (loToken.StringOfEquals("refresh_token","",1) = 1)) THEN
? "Missing value: refresh_token"
RELEASE loOauth
RELEASE loToken
CANCEL
ENDIF
* Use a private working directory accessible only to this application/user.
lnSuccess = loToken.WriteFile("jira-token.json")
IF (lnSuccess <> 1) THEN
? "Could not save the token file."
RELEASE loOauth
RELEASE loToken
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = loToken.StringOf("access_token")
loHttp.Accept = "application/json"
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpNoBody("GET","https://api.atlassian.com/oauth/token/accessible-resources",loResp)
IF (loResp.StatusCode = 0) THEN
? "No HTTP response was received. Check connectivity and TLS settings."
RELEASE loOauth
RELEASE loToken
RELEASE loHttp
RELEASE loResp
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 loOauth
RELEASE loToken
RELEASE loHttp
RELEASE loResp
CANCEL
ENDIF
loSites = CreateObject('Chilkat.JsonArray')
lnSuccess = loSites.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Expected an array of accessible sites."
RELEASE loOauth
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loSites
CANCEL
ENDIF
i = 0
loSite = CreateObject('Chilkat.JsonObject')
DO WHILE (i < loSites.Size)
loSite = loSites.ObjectAt(i)
IF (loSites.LastMethodSuccess <> 1) THEN
? "Invalid site entry."
RELEASE loOauth
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loSites
RELEASE loSite
CANCEL
ENDIF
? loSite.StringOf("name") + " " + loSite.StringOf("url") + " Cloud ID: " + loSite.StringOf("id")
RELEASE loSite
i = i + 1
ENDDO
? "Tokens saved. Select the intended site's Cloud ID for subsequent API requests."
RELEASE loOauth
RELEASE loToken
RELEASE loHttp
RELEASE loResp
RELEASE loSites
RELEASE loSite