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 Unicode C++ Downloads
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkFileAccessW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkJsonObjectW token;
success = token.LoadFile(L"jira-token.json");
if (success != true) {
wprintf(L"Could not read the private token file.\n");
return;
}
if ((token.JsonTypeOf(L"refresh_token") != 1) || (token.StringOfEquals(L"refresh_token",L"",true) == true)) {
wprintf(L"Missing value: refresh_token\n");
return;
}
CkJsonObjectW request;
request.UpdateString(L"grant_type",L"refresh_token");
request.UpdateString(L"client_id",L"YOUR_CLIENT_ID");
request.UpdateString(L"client_secret",L"YOUR_CLIENT_SECRET");
request.UpdateString(L"refresh_token",token.stringOf(L"refresh_token"));
CkHttpW http;
http.put_RequireSslCertVerify(true);
CkHttpResponseW resp;
CkJsonObjectW reply;
success = http.HttpJson(L"POST",L"https://auth.atlassian.com/oauth/token",request,L"application/json",resp);
if (resp.get_StatusCode() == 0) {
wprintf(L"No HTTP response was received. Check connectivity and TLS settings.\n");
return;
}
if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
wprintf(L"Request failed. HTTP %d\n",resp.get_StatusCode());
if (resp.get_StatusCode() == 429) {
wprintf(L"Rate limited. Retry-After: %s\n",resp.getHeaderField(L"Retry-After"));
}
return;
}
success = reply.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Expected a JSON object in the response.\n");
return;
}
if ((reply.JsonTypeOf(L"access_token") != 1) || (reply.StringOfEquals(L"access_token",L"",true) == true)) {
wprintf(L"Missing value: access_token\n");
return;
}
if ((reply.JsonTypeOf(L"refresh_token") != 1) || (reply.StringOfEquals(L"refresh_token",L"",true) == true)) {
wprintf(L"Missing value: refresh_token\n");
return;
}
success = reply.WriteFile(L"jira-token.new.json");
if (success != true) {
wprintf(L"Could not save the rotated token; do not discard the response from memory.\n");
return;
}
success = reply.WriteFile(L"jira-token.json");
if (success != true) {
wprintf(L"Could not update the token cache. Recover from jira-token.new.json; it contains the rotated token.\n");
return;
}
CkFileAccessW files;
success = files.FileDelete(L"jira-token.new.json");
if (success != true) {
wprintf(L"Token cache updated, but the recovery copy could not be removed from the private directory.\n");
return;
}
wprintf(L"Access and refresh tokens updated. Expires in seconds: %d\n",reply.IntOf(L"expires_in"));
}