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

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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Oauth
oleobject loo_Client
oleobject loo_Fresh
oleobject loo_Obtained

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Token
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Token.LoadFile("qa_data/tokens/office365.json")
if li_Success <> 1 then
    Write-Debug "Could not read qa_data/tokens/office365.json from the private working directory."
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("refresh_token") <> 1) OR (loo_Token.StringOfEquals("refresh_token","",1) = 1) then
    Write-Debug "Authorize the account for offline access first."
    destroy loo_Token
    return
end if

loo_Oauth = create oleobject
li_rc = loo_Oauth.ConnectToNewObject("Chilkat.OAuth2")

loo_Oauth.TokenEndpoint = "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.
loo_Client = create oleobject
li_rc = loo_Client.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Client.LoadFile("microsoft-oauth-client.json")
if li_Success <> 1 then
    Write-Debug "Could not load the private OAuth client configuration."
    destroy loo_Token
    destroy loo_Oauth
    destroy loo_Client
    return
end if

if (loo_Client.JsonTypeOf("client_id") <> 1) OR (loo_Client.StringOfEquals("client_id","",1) = 1) then
    Write-Debug "A matching client_id is required."
    destroy loo_Token
    destroy loo_Oauth
    destroy loo_Client
    return
end if

loo_Oauth.ClientId = loo_Client.StringOf("client_id")
if loo_Client.JsonTypeOf("client_secret") = 1 then
    loo_Oauth.ClientSecret = loo_Client.StringOf("client_secret")
end if

//  Use the same registration and authority as the original authorization.
loo_Oauth.RefreshToken = loo_Token.StringOf("refresh_token")
li_Success = loo_Oauth.RefreshAccessToken()
if li_Success <> 1 then
    Write-Debug "Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent."
    destroy loo_Token
    destroy loo_Oauth
    destroy loo_Client
    return
end if

loo_Fresh = create oleobject
li_rc = loo_Fresh.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Fresh.Load(loo_Oauth.AccessTokenResponse)
if li_Success <> 1 then
    Write-Debug "Could not parse the refresh response."
    destroy loo_Token
    destroy loo_Oauth
    destroy loo_Client
    destroy loo_Fresh
    return
end if

if (loo_Fresh.JsonTypeOf("access_token") <> 1) OR (loo_Fresh.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "The refresh response did not contain an access token."
    destroy loo_Token
    destroy loo_Oauth
    destroy loo_Client
    destroy loo_Fresh
    return
end if

if (loo_Fresh.JsonTypeOf("refresh_token") <> 1) OR (loo_Fresh.StringOfEquals("refresh_token","",1) = 1) then
    loo_Fresh.UpdateString("refresh_token",loo_Token.StringOf("refresh_token"))
end if

if (loo_Fresh.HasMember("scope") <> 1) AND (loo_Token.JsonTypeOf("scope") = 1) then
    loo_Fresh.UpdateString("scope",loo_Token.StringOf("scope"))
end if

//  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.
loo_Obtained = create oleobject
li_rc = loo_Obtained.ConnectToNewObject("Chilkat.CkDateTime")

loo_Obtained.SetFromCurrentSystemTime()
loo_Fresh.UpdateString("obtained_at",loo_Obtained.GetAsTimestamp(0))
li_Success = loo_Fresh.WriteFile("qa_data/tokens/office365.json")
if li_Success <> 1 then
    Write-Debug "Could not save the refreshed credentials."
    destroy loo_Token
    destroy loo_Oauth
    destroy loo_Client
    destroy loo_Fresh
    destroy loo_Obtained
    return
end if

Write-Debug "Refreshed the access token."


destroy loo_Token
destroy loo_Oauth
destroy loo_Client
destroy loo_Fresh
destroy loo_Obtained