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

  1. Setup a web server running PHP.
  2. Copy files from Github to "http://localhost/openapi-samples-js/authentication/oauth2-code-flow/redirect/".
  3. The redirect URL is now "http://localhost/openapi-samples-js/authentication/oauth2-code-flow/redirect/". Add this as redirect below.
  4. Optional: Create your own app with the redirect URL of your choice.
  5. Optional: If you've created your own app: modify server-config.php, so it contains the correct app key, secret and redirect URL.
See the PHP Repository of Saxobank for more samples with PHP.

Steps for NodeJs

  1. Clone this repository and navigate to \openapi-samples-js\authentication\oauth2-code-flow\redirect\backend-js\
  2. Get the required modules using "npm install".
  3. Launch the express web server with "node server.js".
  4. The redirect URL is now "http://localhost:1337/". Add this as redirect below.
  5. Optional: Create your own app with the redirect URL of your choice.
  6. 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.