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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 nvarchar(4000)
DECLARE @sTmp2 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
DECLARE @oauth int
EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @oauth, 'AuthorizationEndpoint', 'https://auth.atlassian.com/authorize'
EXEC sp_OASetProperty @oauth, 'TokenEndpoint', 'https://auth.atlassian.com/oauth/token'
EXEC sp_OASetProperty @oauth, 'ClientId', 'YOUR_CLIENT_ID'
EXEC sp_OASetProperty @oauth, 'ClientSecret', 'YOUR_CLIENT_SECRET'
EXEC sp_OASetProperty @oauth, 'LocalHost', '127.0.0.1'
EXEC sp_OASetProperty @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.
EXEC sp_OASetProperty @oauth, 'UncommonOptions', 'NO_OAUTH2_SCOPE'
EXEC sp_OASetProperty @oauth, 'UseBasicAuth', 0
EXEC sp_OASetProperty @oauth, 'CodeChallenge', 0
EXEC sp_OASetProperty @oauth, 'Scope', 'read:jira-work write:jira-work read:jira-user offline_access'
EXEC sp_OAMethod @oauth, 'AddAuthQueryParam', @success OUT, 'audience', 'api.atlassian.com'
IF @success <> 1
BEGIN
PRINT 'Operation failed.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAMethod @oauth, 'AddAuthQueryParam', @success OUT, 'prompt', 'consent'
IF @success <> 1
BEGIN
PRINT 'Operation failed.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
-- Chilkat generates state and validates it when the browser redirects back.
DECLARE @authUrl nvarchar(4000)
EXEC sp_OAMethod @oauth, 'StartAuth', @authUrl OUT
EXEC sp_OAGetProperty @oauth, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not start the local authorization listener.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAMethod @oauth, 'LaunchBrowser', @success OUT, @authUrl
IF @success <> 1
BEGIN
EXEC sp_OAMethod @oauth, 'Cancel', @success OUT
PRINT 'Could not open the system browser.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @waited int
SELECT @waited = 0
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
WHILE ((@iTmp0 < 3) and (@waited < 180000))
BEGIN
EXEC sp_OAMethod @oauth, 'SleepMs', NULL, 200
SELECT @waited = @waited + 200
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 < 3
BEGIN
EXEC sp_OAMethod @oauth, 'Cancel', @success OUT
PRINT 'Authorization timed out.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 <> 3
BEGIN
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
PRINT 'Authorization did not complete. Flow state: ' + @iTmp0
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
EXEC sp_OAGetProperty @oauth, 'AccessTokenResponse', @sTmp0 OUT
EXEC sp_OAMethod @token, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not parse the token response.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Missing value: access_token'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'refresh_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Missing value: refresh_token'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
-- Use a private working directory accessible only to this application/user.
EXEC sp_OAMethod @token, 'WriteFile', @success OUT, 'jira-token.json'
IF @success <> 1
BEGIN
PRINT 'Could not save the token file.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
EXEC sp_OASetProperty @http, 'Accept', 'application/json'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', 'https://api.atlassian.com/oauth/token/accessible-resources', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No HTTP response was received. Check connectivity and TLS settings.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
IF (@success <> 1) or (@iTmp0 < 200) or (@iTmp1 >= 300)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Request failed. HTTP ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 429
BEGIN
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Rate limited. Retry-After: ' + @sTmp0
END
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @sites int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @sites OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @sites, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected an array of accessible sites.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sites
RETURN
END
DECLARE @i int
SELECT @i = 0
DECLARE @site int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @site OUT
EXEC sp_OAGetProperty @sites, 'Size', @iTmp0 OUT
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OAMethod @sites, 'ObjectAt', @site OUT, @i
EXEC sp_OAGetProperty @sites, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Invalid site entry.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sites
EXEC @hr = sp_OADestroy @site
RETURN
END
EXEC sp_OAMethod @site, 'StringOf', @sTmp0 OUT, 'name'
EXEC sp_OAMethod @site, 'StringOf', @sTmp1 OUT, 'url'
EXEC sp_OAMethod @site, 'StringOf', @sTmp2 OUT, 'id'
PRINT @sTmp0 + ' ' + @sTmp1 + ' Cloud ID: ' + @sTmp2
EXEC @hr = sp_OADestroy @site
SELECT @i = @i + 1
END
PRINT 'Tokens saved. Select the intended site''s Cloud ID for subsequent API requests.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @sites
EXEC @hr = sp_OADestroy @site
END
GO