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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set oauth = Server.CreateObject("Chilkat.OAuth2")
oauth.AuthorizationEndpoint = "https://auth.atlassian.com/authorize"
oauth.TokenEndpoint = "https://auth.atlassian.com/oauth/token"
oauth.ClientId = "YOUR_CLIENT_ID"
oauth.ClientSecret = "YOUR_CLIENT_SECRET"
oauth.LocalHost = "127.0.0.1"
oauth.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.
oauth.UncommonOptions = "NO_OAUTH2_SCOPE"
oauth.UseBasicAuth = 0
oauth.CodeChallenge = 0
oauth.Scope = "read:jira-work write:jira-work read:jira-user offline_access"
success = oauth.AddAuthQueryParam("audience","api.atlassian.com")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Operation failed.") & "</pre>"
Response.End
End If
success = oauth.AddAuthQueryParam("prompt","consent")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Operation failed.") & "</pre>"
Response.End
End If
' Chilkat generates state and validates it when the browser redirects back.
authUrl = oauth.StartAuth()
If (oauth.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not start the local authorization listener.") & "</pre>"
Response.End
End If
success = oauth.LaunchBrowser(authUrl)
If (success <> 1) Then
success = oauth.Cancel()
Response.Write "<pre>" & Server.HTMLEncode( "Could not open the system browser.") & "</pre>"
Response.End
End If
waited = 0
Do While ((oauth.AuthFlowState < 3) And (waited < 180000))
oauth.SleepMs 200
waited = waited + 200
Loop
If (oauth.AuthFlowState < 3) Then
success = oauth.Cancel()
Response.Write "<pre>" & Server.HTMLEncode( "Authorization timed out.") & "</pre>"
Response.End
End If
If (oauth.AuthFlowState <> 3) Then
Response.Write "<pre>" & Server.HTMLEncode( "Authorization did not complete. Flow state: " & oauth.AuthFlowState) & "</pre>"
Response.End
End If
set token = Server.CreateObject("Chilkat.JsonObject")
success = token.Load(oauth.AccessTokenResponse)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not parse the token response.") & "</pre>"
Response.End
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing value: access_token") & "</pre>"
Response.End
End If
If ((token.JsonTypeOf("refresh_token") <> 1) Or (token.StringOfEquals("refresh_token","",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing value: refresh_token") & "</pre>"
Response.End
End If
' Use a private working directory accessible only to this application/user.
success = token.WriteFile("jira-token.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not save the token file.") & "</pre>"
Response.End
End If
set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = token.StringOf("access_token")
http.Accept = "application/json"
set resp = Server.CreateObject("Chilkat.HttpResponse")
success = http.HttpNoBody("GET","https://api.atlassian.com/oauth/token/accessible-resources",resp)
If (resp.StatusCode = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response was received. Check connectivity and TLS settings.") & "</pre>"
Response.End
End If
If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Request failed. HTTP " & resp.StatusCode) & "</pre>"
If (resp.StatusCode = 429) Then
Response.Write "<pre>" & Server.HTMLEncode( "Rate limited. Retry-After: " & resp.GetHeaderField("Retry-After")) & "</pre>"
End If
Response.End
End If
set sites = Server.CreateObject("Chilkat.JsonArray")
success = sites.Load(resp.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected an array of accessible sites.") & "</pre>"
Response.End
End If
i = 0
set site = Server.CreateObject("Chilkat.JsonObject")
Do While (i < sites.Size)
' site is a Chilkat.JsonObject
Set site = sites.ObjectAt(i)
If (sites.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid site entry.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( site.StringOf("name") & " " & site.StringOf("url") & " Cloud ID: " & site.StringOf("id")) & "</pre>"
i = i + 1
Loop
Response.Write "<pre>" & Server.HTMLEncode( "Tokens saved. Select the intended site's Cloud ID for subsequent API requests.") & "</pre>"
%>
</body>
</html>