OAuth2 Code Flow Sample
This is a demo on how to get a token using the authorization code flow.The code flow is the preferred flow, because it allows refreshing the token, so your app can use the API for a long time.
This page redirects you to the login website, to authenticate and retrieve a code.
The code flow requires a backend, so the app secret remains secret. This example contains two flavours, PHP and NodeJs.
Steps for PHP
- Setup a web server running PHP.
- Copy files from Github to "http://localhost/openapi-samples-js/authentication/oauth2-code-flow/redirect/".
- The redirect URL is now "http://localhost/openapi-samples-js/authentication/oauth2-code-flow/redirect/". Add this as redirect below.
- Optional: Create your own app with the redirect URL of your choice.
- Optional: If you've created your own app: modify server-config.php, so it contains the correct app key, secret and redirect URL.
Steps for NodeJs
- Clone this repository and navigate to \openapi-samples-js\authentication\oauth2-code-flow\redirect\backend-js\
- Get the required modules using "npm install".
- Launch the express web server with "node server.js".
- The redirect URL is now "http://localhost:1337/". Add this as redirect below.
- Optional: Create your own app with the redirect URL of your choice.
- Optional: If you've created your own app: modify server.js, so it contains the correct app key, secret and redirect URL.
Response:
Click button to launch function.
JS code:
Click button to show code.