DataFlex Requires Chilkat v11.0.0+
DataFlex
Refresh a Gmail OAuth access token
See more GMail REST API Examples
Use the refresh token and the same Desktop app client that issued it. Run in the private directory used by the authorization example. Preserve the refresh token if Google omits it from the response. Refreshing continues the existing grant. Request additional scopes through interactive authorization.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
Handle hoOauth
Handle hoFresh
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComLoadFile Of hoToken "gmail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read gmail-token.json from the private working directory."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "refresh_token" To iTemp1
Get ComStringOfEquals Of hoToken "refresh_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Authorize the account for offline access first."
Procedure_Return
End
Get Create (RefClass(cComChilkatOAuth2)) To hoOauth
If (Not(IsComObjectCreated(hoOauth))) Begin
Send CreateComObject of hoOauth
End
Set ComTokenEndpoint Of hoOauth To "https://oauth2.googleapis.com/token"
Set ComClientId Of hoOauth To "YOUR_DESKTOP_CLIENT_ID"
Set ComClientSecret Of hoOauth To "YOUR_DESKTOP_CLIENT_SECRET"
Get ComStringOf Of hoToken "refresh_token" To sTemp1
Set ComRefreshToken Of hoOauth To sTemp1
Get ComRefreshAccessToken Of hoOauth To iSuccess
If (iSuccess <> True) Begin
Showln "Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoFresh
If (Not(IsComObjectCreated(hoFresh))) Begin
Send CreateComObject of hoFresh
End
Get ComAccessTokenResponse Of hoOauth To sTemp1
Get ComLoad Of hoFresh sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not parse the refresh response."
Procedure_Return
End
Get ComJsonTypeOf Of hoFresh "access_token" To iTemp1
Get ComStringOfEquals Of hoFresh "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "The refresh response did not contain an access token."
Procedure_Return
End
Get ComJsonTypeOf Of hoFresh "refresh_token" To iTemp1
Get ComStringOfEquals Of hoFresh "refresh_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Get ComStringOf Of hoToken "refresh_token" To sTemp1
Get ComUpdateString Of hoFresh "refresh_token" sTemp1 To iSuccess
End
Get ComHasMember Of hoFresh "scope" To bTemp1
Get ComJsonTypeOf Of hoToken "scope" To iTemp1
If ((bTemp1 <> True) And (iTemp1 = 1)) Begin
Get ComStringOf Of hoToken "scope" To sTemp1
Get ComUpdateString Of hoFresh "scope" sTemp1 To iSuccess
End
Get ComWriteFile Of hoFresh "gmail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not save the refreshed credentials."
Procedure_Return
End
Showln "Refreshed the access token."
End_Procedure