KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to verify OAuth2 token validity with OAuth2Provider Class
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: May 20, 2025

The OAuth2Provider class is part of the built-in 4D NetKit component that lets developers interact with 3rd party web services. It is great for requesting authentication tokens for use with Google REST API or Microsoft Graph API. Each instantiated OAuth2Provider object has a function called “isTokenValid” (20 R8 or newer) that can be used to verify if a token is missing or expired. For instance:

$myOAuth2:=cs.NetKit.OAuth2Provider.new($paramObj)

If (Not($myOAuth2.isTokenValid()))
   $newToken:=$myOAuth2.getToken()
End if

In the above example, after the OAuth2Provider instance is created, the validity of its token is verified, and when the token is detected to be missing or expired, the “getToken” function is invoked to request a new token. This new token can then be saved in a shared object for subsequent uses.