PureBasic
PureBasic
Refresh Expiring OAuth2 Access Token for Azure Registered App
See more OAuth2 Examples
Shows how to renew an Azure App's access token using the refresh token when it's near expiration.Chilkat PureBasic Downloads
IncludeFile "CkFileAccess.pb"
IncludeFile "CkOAuth2.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"
Procedure ChilkatExample()
success.i = 0
; We previously obtained an access token and saved the JSON to a file using this example:
; Get OAuth2 Access Token for Azure Registered App
; This example will examine the JSON and expiration date, and if near expiration will
; refresh the access token.
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(json,"qa_data/tokens/_myAzureApp.json")
If success <> 1
Debug "Failed to load the access token."
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
; The contents of the JSON look like this:
; {
; "token_type": "Bearer",
; "scope": "User.Read Mail.ReadWrite Mail.Send",
; "expires_in": 3600,
; "ext_expires_in": 0,
; "access_token": "EwBAA8l6B...",
; "refresh_token": "MCRMdbe6Cd...",
; "id_token": "eyJ0eXAiOiJ...",
; "expires_on": "1494112119"
; }
; The "expires_on" value is a Unix time.
dtExpire.i = CkDateTime::ckCreate()
If dtExpire.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkDateTime::ckSetFromUnixTime(dtExpire,0,CkJsonObject::ckIntOf(json,"expires_on"))
; If this date/time expires within 10 minutes of the current system time, refresh the token.
If CkDateTime::ckExpiresWithin(dtExpire,10,"minutes") <> 1
Debug "No need to refresh, the access token won't expire within the next 10 minutes."
CkJsonObject::ckDispose(json)
CkDateTime::ckDispose(dtExpire)
ProcedureReturn
EndIf
; OK, we need to refresh the access token..
oauth2.i = CkOAuth2::ckCreate()
If oauth2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Note: The endpoint depends on the Azure App Registration.
; See How to Choose the Correct Endpoints for your Azure App Registration
CkOAuth2::setCkTokenEndpoint(oauth2, "https://login.microsoftonline.com/common/oauth2/v2.0/token")
; Use your client ID.
CkOAuth2::setCkClientId(oauth2, "CLIENT_ID")
; Get the existing refresh token.
CkOAuth2::setCkRefreshToken(oauth2, CkJsonObject::ckStringOf(json,"refresh_token"))
; Send the HTTP POST to refresh the access token.
success = CkOAuth2::ckRefreshAccessToken(oauth2)
If success = 0
Debug CkOAuth2::ckLastErrorText(oauth2)
CkJsonObject::ckDispose(json)
CkDateTime::ckDispose(dtExpire)
CkOAuth2::ckDispose(oauth2)
ProcedureReturn
EndIf
Debug "OAuth2 authorization granted!"
Debug "Access Token = " + CkOAuth2::ckAccessToken(oauth2)
; Get the full JSON response:
CkJsonObject::ckLoad(json,CkOAuth2::ckAccessTokenResponse(oauth2))
CkJsonObject::setCkEmitCompact(json, 0)
; If an "expires_on" member does not exist, then add the JSON member by
; getting the current system date/time and adding the "expires_in" seconds.
; This way we'll know when the token expires.
If CkJsonObject::ckHasMember(json,"expires_on") <> 1
CkDateTime::ckSetFromCurrentSystemTime(dtExpire)
CkDateTime::ckAddSeconds(dtExpire,CkJsonObject::ckIntOf(json,"expires_in"))
CkJsonObject::ckAppendString(json,"expires_on",CkDateTime::ckGetAsUnixTimeStr(dtExpire,0))
EndIf
Debug CkJsonObject::ckEmit(json)
; Save the new access token JSON to a file for future requests.
fac.i = CkFileAccess::ckCreate()
If fac.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkFileAccess::ckWriteEntireTextFile(fac,"qa_data/tokens/_myAzureApp.json",CkJsonObject::ckEmit(json),"utf-8",0)
CkJsonObject::ckDispose(json)
CkDateTime::ckDispose(dtExpire)
CkOAuth2::ckDispose(oauth2)
CkFileAccess::ckDispose(fac)
ProcedureReturn
EndProcedure