PureBasic Requires Chilkat v11.0.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkOAuth2.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
token.i = CkJsonObject::ckCreate()
If token.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(token,"gmail-token.json")
If success <> 1
Debug "Could not read gmail-token.json from the private working directory."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(token,"refresh_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"refresh_token","",1) = 1)
Debug "Authorize the account for offline access first."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
oauth.i = CkOAuth2::ckCreate()
If oauth.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkOAuth2::setCkTokenEndpoint(oauth, "https://oauth2.googleapis.com/token")
CkOAuth2::setCkClientId(oauth, "YOUR_DESKTOP_CLIENT_ID")
CkOAuth2::setCkClientSecret(oauth, "YOUR_DESKTOP_CLIENT_SECRET")
CkOAuth2::setCkRefreshToken(oauth, CkJsonObject::ckStringOf(token,"refresh_token"))
success = CkOAuth2::ckRefreshAccessToken(oauth)
If success <> 1
Debug "Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent."
CkJsonObject::ckDispose(token)
CkOAuth2::ckDispose(oauth)
ProcedureReturn
EndIf
fresh.i = CkJsonObject::ckCreate()
If fresh.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(fresh,CkOAuth2::ckAccessTokenResponse(oauth))
If success <> 1
Debug "Could not parse the refresh response."
CkJsonObject::ckDispose(token)
CkOAuth2::ckDispose(oauth)
CkJsonObject::ckDispose(fresh)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(fresh,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(fresh,"access_token","",1) = 1)
Debug "The refresh response did not contain an access token."
CkJsonObject::ckDispose(token)
CkOAuth2::ckDispose(oauth)
CkJsonObject::ckDispose(fresh)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(fresh,"refresh_token") <> 1) OR (CkJsonObject::ckStringOfEquals(fresh,"refresh_token","",1) = 1)
CkJsonObject::ckUpdateString(fresh,"refresh_token",CkJsonObject::ckStringOf(token,"refresh_token"))
EndIf
If (CkJsonObject::ckHasMember(fresh,"scope") <> 1) AND (CkJsonObject::ckJsonTypeOf(token,"scope") = 1)
CkJsonObject::ckUpdateString(fresh,"scope",CkJsonObject::ckStringOf(token,"scope"))
EndIf
success = CkJsonObject::ckWriteFile(fresh,"gmail-token.json")
If success <> 1
Debug "Could not save the refreshed credentials."
CkJsonObject::ckDispose(token)
CkOAuth2::ckDispose(oauth)
CkJsonObject::ckDispose(fresh)
ProcedureReturn
EndIf
Debug "Refreshed the access token."
CkJsonObject::ckDispose(token)
CkOAuth2::ckDispose(oauth)
CkJsonObject::ckDispose(fresh)
ProcedureReturn
EndProcedure