With 4D v19 R3, connecting your application to the Microsoft Graph API through OAuth 2.0 has become incredibly convenient using 4D NetKit.
Now, with 4D v20 R2, you can seamlessly obtain an OAuth 2.0 token for Google API or other OAuth 2.0 servers. And even better, to further enhance the integration of this authentication process into your application, we have introduced a feature that allows you to display your HTML page directly in your web browser once the token is received.
Keep reading for all the details!
HDI Connect to GitHub using OAuth 2.0
Prerequisites
Before getting a token from the Google API, the first step is to obtain OAuth 2.0 client credentials from the Google API Console. It establishes a trust relationship between your app and the Google API. Trust is unidirectional: your app trusts the Google API, not vice versa.
This tutorial by Google is an excellent resource for understanding how to use OAuth 2.0 to access Google APIs.
Registration integrates your application with the Google API Console and establishes the information that it uses to get tokens, including:
- a Client ID: A unique identifier assigned by the Google API Console.
- a Redirect URI/URL: One or more endpoints where your app will receive responses from the Google API Console.
- a Client Secret: A secret key that your app uses to authenticate with the Google API Console.
getTING AN access token from Google
You just need to use the New OAuth2 provider command or the cs.NetKit.OAuth2Provider class with the name attribute equal to “Google” to get your token.
cs.NetKit.OAuth2Provider class with all the information given during the registration:
var $oAuth2 : cs.NetKit.OAuth2Provider
var $param : Object
$param:={name:"Google"; \
permission:"signedIn"; \
clientId:"499730024306-rfub401kvb2794llikrtb4fphigrggt7.apps.googleusercontent.com"; \
clientSecret:"fc1kwxb6NMzEKi9Ka_fjvB2Z"; \
redirectURI:"http://127.0.0.1:50993/authorize/"; \
scope:"https://mail.google.com/"}
$oAuth2:=cs.NetKit.OAuth2Provider.new($param)
$token:=$oAuth2.getToken()
getTING AN access token from other servers
You can use the New OAuth2 provider command or the cs.NetKit.OAuth2Provider class with other OAuth 2.0 servers than Microsoft or Google. You just need to fill the authenticateURI and the tokenURI with the specific URIs of your OAuth 2.0 server and verify that the authorization code returned is in JSON format.
For example, you can connect your application to GitHub with the code below:
var $oAuth2 : cs.NetKit.OAuth2Provider
var $param : Object
$param:={permission:"signedIn"; \
clientId:"734e0b59xxx"; \
clientSecret:"125a576xxx"; \
redirectURI:"http://localhost:50993/authorize/"; \
scope:"repo, user"; \
authenticateURI:"https://github.com/login/oauth/authorize"; \
tokenURI:"https://github.com/login/oauth/access_token"}
$oAuth2:=cs.NetKit.OAuth2Provider.new($param)
$token:=$oAuth2.getToken()
Custom the result pages
To display your own page at the end of the authentication, you need to use authenticationPage and authenticationErrorPage attributes:
$param.authenticationPage:=Folder(fk web root folder).file("authentication.htm")
$param.authenticationErrorPage:=Folder(fk web root folder).file("error.htm")
We constantly strive to provide our users with the best possible experience, and we encourage you to share your thoughts and feedback on the 4D forum. Your feedback helps us better understand your needs and continuously improve our products and services.