Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

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 PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonArray.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkOAuth2.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    oauth.i = CkOAuth2::ckCreate()
    If oauth.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkOAuth2::setCkAuthorizationEndpoint(oauth, "https://auth.atlassian.com/authorize")
    CkOAuth2::setCkTokenEndpoint(oauth, "https://auth.atlassian.com/oauth/token")
    CkOAuth2::setCkClientId(oauth, "YOUR_CLIENT_ID")
    CkOAuth2::setCkClientSecret(oauth, "YOUR_CLIENT_SECRET")
    CkOAuth2::setCkLocalHost(oauth, "127.0.0.1")
    CkOAuth2::setCkListenPort(oauth, 3017)
    ;  Register exactly http://127.0.0.1:3017/ in the developer console.
    ;  Keep scope on the authorization request, not the authorization-code exchange.
    CkOAuth2::setCkUncommonOptions(oauth, "NO_OAUTH2_SCOPE")
    CkOAuth2::setCkUseBasicAuth(oauth, 0)
    CkOAuth2::setCkCodeChallenge(oauth, 0)
    CkOAuth2::setCkScope(oauth, "read:jira-work write:jira-work read:jira-user offline_access")
    success = CkOAuth2::ckAddAuthQueryParam(oauth,"audience","api.atlassian.com")
    If success <> 1
        Debug "Operation failed."
        CkOAuth2::ckDispose(oauth)
        ProcedureReturn
    EndIf

    success = CkOAuth2::ckAddAuthQueryParam(oauth,"prompt","consent")
    If success <> 1
        Debug "Operation failed."
        CkOAuth2::ckDispose(oauth)
        ProcedureReturn
    EndIf

    ;  Chilkat generates state and validates it when the browser redirects back.
    authUrl.s = CkOAuth2::ckStartAuth(oauth)
    If CkOAuth2::ckLastMethodSuccess(oauth) <> 1
        Debug "Could not start the local authorization listener."
        CkOAuth2::ckDispose(oauth)
        ProcedureReturn
    EndIf

    success = CkOAuth2::ckLaunchBrowser(oauth,authUrl)
    If success <> 1
        CkOAuth2::ckCancel(oauth)
        Debug "Could not open the system browser."
        CkOAuth2::ckDispose(oauth)
        ProcedureReturn
    EndIf

    waited.i = 0
    While ((CkOAuth2::ckAuthFlowState(oauth) < 3) AND (waited < 180000))
        CkOAuth2::ckSleepMs(oauth,200)
        waited = waited + 200
    Wend
    If CkOAuth2::ckAuthFlowState(oauth) < 3
        CkOAuth2::ckCancel(oauth)
        Debug "Authorization timed out."
        CkOAuth2::ckDispose(oauth)
        ProcedureReturn
    EndIf

    If CkOAuth2::ckAuthFlowState(oauth) <> 3
        Debug "Authorization did not complete. Flow state: " + Str(CkOAuth2::ckAuthFlowState(oauth))
        CkOAuth2::ckDispose(oauth)
        ProcedureReturn
    EndIf

    token.i = CkJsonObject::ckCreate()
    If token.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoad(token,CkOAuth2::ckAccessTokenResponse(oauth))
    If success <> 1
        Debug "Could not parse the token response."
        CkOAuth2::ckDispose(oauth)
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"access_token","",1) = 1)
        Debug "Missing value: access_token"
        CkOAuth2::ckDispose(oauth)
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(token,"refresh_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"refresh_token","",1) = 1)
        Debug "Missing value: refresh_token"
        CkOAuth2::ckDispose(oauth)
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    ;  Use a private working directory accessible only to this application/user.
    success = CkJsonObject::ckWriteFile(token,"jira-token.json")
    If success <> 1
        Debug "Could not save the token file."
        CkOAuth2::ckDispose(oauth)
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(token,"access_token"))
    CkHttp::setCkAccept(http, "application/json")
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpNoBody(http,"GET","https://api.atlassian.com/oauth/token/accessible-resources",resp)
    If CkHttpResponse::ckStatusCode(resp) = 0
        Debug "No HTTP response was received. Check connectivity and TLS settings."
        CkOAuth2::ckDispose(oauth)
        CkJsonObject::ckDispose(token)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    If (success <> 1) OR (CkHttpResponse::ckStatusCode(resp) < 200) OR (CkHttpResponse::ckStatusCode(resp) >= 300)
        Debug "Request failed. HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
        If CkHttpResponse::ckStatusCode(resp) = 429
            Debug "Rate limited. Retry-After: " + CkHttpResponse::ckGetHeaderField(resp,"Retry-After")
        EndIf

        CkOAuth2::ckDispose(oauth)
        CkJsonObject::ckDispose(token)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    sites.i = CkJsonArray::ckCreate()
    If sites.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonArray::ckLoad(sites,CkHttpResponse::ckBodyStr(resp))
    If success <> 1
        Debug "Expected an array of accessible sites."
        CkOAuth2::ckDispose(oauth)
        CkJsonObject::ckDispose(token)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonArray::ckDispose(sites)
        ProcedureReturn
    EndIf

    i.i = 0
    site.i = CkJsonObject::ckCreate()
    If site.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    While (i < CkJsonArray::ckSize(sites))
        site = CkJsonArray::ckObjectAt(sites,i)
        If CkJsonArray::ckLastMethodSuccess(sites) <> 1
            Debug "Invalid site entry."
            CkOAuth2::ckDispose(oauth)
            CkJsonObject::ckDispose(token)
            CkHttp::ckDispose(http)
            CkHttpResponse::ckDispose(resp)
            CkJsonArray::ckDispose(sites)
            CkJsonObject::ckDispose(site)
            ProcedureReturn
        EndIf

        Debug CkJsonObject::ckStringOf(site,"name") + "  " + CkJsonObject::ckStringOf(site,"url") + "  Cloud ID: " + CkJsonObject::ckStringOf(site,"id")
        CkJsonObject::ckDispose(site)

        i = i + 1
    Wend
    Debug "Tokens saved. Select the intended site's Cloud ID for subsequent API requests."


    CkOAuth2::ckDispose(oauth)
    CkJsonObject::ckDispose(token)
    CkHttp::ckDispose(http)
    CkHttpResponse::ckDispose(resp)
    CkJsonArray::ckDispose(sites)
    CkJsonObject::ckDispose(site)


    ProcedureReturn
EndProcedure