Microsoft 365: Refresh Mail OAuth2 Tokens
See more Office365 Examples
Exchange a saved delegated refresh token for a new access token without displaying a browser. Use the same app registration and authority as the original authorization. Save any rotated refresh token and the new lifetime information. On a rejected or malformed response, this example preserves the previous cache; an expired/revoked grant or interaction-required response needs interactive authorization again.
Read and update qa_data/tokens/office365.json in a private working directory. Serialize access to the cache; production storage should support atomic replacement and protect credentials. A successful refresh does not enable a disabled mail protocol. Client-credentials tokens have no refresh token; request a new application token instead.
Put {"client_id":"YOUR_APPLICATION_ID"} in private microsoft-oauth-client.json for a public desktop app. An existing confidential-client registration must also supply its client_secret from protected configuration; never distribute that secret in a public app. Replace common in the token endpoint with your tenant ID when matching a tenant-specific authorization.
Authorize first: desktop PKCE example. This is delegated background access after consent, not permanently unattended application authentication.
Microsoft reference documentation
Chilkat Unicode C Downloads
#include <C_CkJsonObjectW.h>
#include <C_CkOAuth2W.h>
#include <C_CkDateTimeW.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObjectW token;
HCkOAuth2W oauth;
HCkJsonObjectW client;
HCkJsonObjectW fresh;
HCkDateTimeW obtained;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(token,L"qa_data/tokens/office365.json");
if (success != TRUE) {
wprintf(L"Could not read qa_data/tokens/office365.json from the private working directory.\n");
CkJsonObjectW_Dispose(token);
return;
}
if ((CkJsonObjectW_JsonTypeOf(token,L"refresh_token") != 1) || (CkJsonObjectW_StringOfEquals(token,L"refresh_token",L"",TRUE) == TRUE)) {
wprintf(L"Authorize the account for offline access first.\n");
CkJsonObjectW_Dispose(token);
return;
}
oauth = CkOAuth2W_Create();
CkOAuth2W_putTokenEndpoint(oauth,L"https://login.microsoftonline.com/common/oauth2/v2.0/token");
// For public clients the private configuration contains only client_id.
// For an existing confidential client it also contains client_secret.
client = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(client,L"microsoft-oauth-client.json");
if (success != TRUE) {
wprintf(L"Could not load the private OAuth client configuration.\n");
CkJsonObjectW_Dispose(token);
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(client);
return;
}
if ((CkJsonObjectW_JsonTypeOf(client,L"client_id") != 1) || (CkJsonObjectW_StringOfEquals(client,L"client_id",L"",TRUE) == TRUE)) {
wprintf(L"A matching client_id is required.\n");
CkJsonObjectW_Dispose(token);
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(client);
return;
}
CkOAuth2W_putClientId(oauth,CkJsonObjectW_stringOf(client,L"client_id"));
if (CkJsonObjectW_JsonTypeOf(client,L"client_secret") == 1) {
CkOAuth2W_putClientSecret(oauth,CkJsonObjectW_stringOf(client,L"client_secret"));
}
// Use the same registration and authority as the original authorization.
CkOAuth2W_putRefreshToken(oauth,CkJsonObjectW_stringOf(token,L"refresh_token"));
success = CkOAuth2W_RefreshAccessToken(oauth);
if (success != TRUE) {
wprintf(L"Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent.\n");
CkJsonObjectW_Dispose(token);
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(client);
return;
}
fresh = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(fresh,CkOAuth2W_accessTokenResponse(oauth));
if (success != TRUE) {
wprintf(L"Could not parse the refresh response.\n");
CkJsonObjectW_Dispose(token);
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(client);
CkJsonObjectW_Dispose(fresh);
return;
}
if ((CkJsonObjectW_JsonTypeOf(fresh,L"access_token") != 1) || (CkJsonObjectW_StringOfEquals(fresh,L"access_token",L"",TRUE) == TRUE)) {
wprintf(L"The refresh response did not contain an access token.\n");
CkJsonObjectW_Dispose(token);
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(client);
CkJsonObjectW_Dispose(fresh);
return;
}
if ((CkJsonObjectW_JsonTypeOf(fresh,L"refresh_token") != 1) || (CkJsonObjectW_StringOfEquals(fresh,L"refresh_token",L"",TRUE) == TRUE)) {
CkJsonObjectW_UpdateString(fresh,L"refresh_token",CkJsonObjectW_stringOf(token,L"refresh_token"));
}
if ((CkJsonObjectW_HasMember(fresh,L"scope") != TRUE) && (CkJsonObjectW_JsonTypeOf(token,L"scope") == 1)) {
CkJsonObjectW_UpdateString(fresh,L"scope",CkJsonObjectW_stringOf(token,L"scope"));
}
// Persist the rotated refresh token. Retain the previous one only if the response omits it.
// Use the new expires_in, not the old response's expiration information.
obtained = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(obtained);
CkJsonObjectW_UpdateString(fresh,L"obtained_at",CkDateTimeW_getAsTimestamp(obtained,FALSE));
success = CkJsonObjectW_WriteFile(fresh,L"qa_data/tokens/office365.json");
if (success != TRUE) {
wprintf(L"Could not save the refreshed credentials.\n");
CkJsonObjectW_Dispose(token);
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(client);
CkJsonObjectW_Dispose(fresh);
CkDateTimeW_Dispose(obtained);
return;
}
wprintf(L"Refreshed the access token.\n");
CkJsonObjectW_Dispose(token);
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(client);
CkJsonObjectW_Dispose(fresh);
CkDateTimeW_Dispose(obtained);
}