Skip to main content

Sample code

Sample code

Sample code are available in the following languages :

NodeJS

The code below get an access token using the client credentials flow and make an API call using that access token

const axios = require("axios");
const clientId = "YOUR_CLIENT_ID";
const clientSecret = "YOUR_CLIENT_SECRET";

(async () => {
    // get an access token
    const response = await axios({
        method: 'POST',
        headers: {
            'content-type': 'application/x-www-form-urlencoded',
            "Authorization": `Basic ${Buffer.from(clientId+ ":" + clientSecret).toString('base64')}`
        },
        data: "grant_type=client_credentials",
        url: 'https://oauth2.bouyguestelecom.fr/token',
    })
    console.log(response.data);

    // make the API call
    const result = await axios({
        method: 'GET',
        headers: {
            "Authorization": `Bearer ${response.data.access_token}`
        },
        url: 'https://oauth2.bouyguestelecom.fr/userinfo'
    })
    console.log(result)
})()

PHP

The code below get an access token using the client credentials flow and make an API call using that access token

<html>
<?php
$clientId = "YOUR_CLIENT_ID";
$clientSecret = "YOUR_CLIENT_SECRET";

$options = array(
    'http' => array(
        'header'  => 
          "Content-type: application/x-www-form-urlencoded\r\n" .
          "Authorization: Basic " . base64_encode($clientId . ":" . $clientSecret) . "\r\n",
        'method'  => 'POST',
        'content' => http_build_query(array('grant_type' => 'client_credentials'))
    )
);
$context  = stream_context_create($options);
$authResult = file_get_contents("https://oauth2.bouyguestelecom.fr/token", false, $context);
$authResultJSON = json_decode($authResult, true);
// var_dump($authResultJSON);

$options = array(
  'http' => array(
      'header'  => 
        "Authorization: Bearer " . $authResultJSON["access_token"] . "\r\n",
      'method'  => 'GET'
  )
);
$context  = stream_context_create($options);
$apiResult = file_get_contents("https://oauth2.bouyguestelecom.fr/verify", false, $context);
// var_dump($apiResult);
?>
<body>
  <div>
    <h2>Authentication result</h2>
    <p><?=$authResult?></p>
  </div>
  <div>
    <h2>API result</h2>
    <p><?=$apiResult?></p>
  </div>
</body>
</html>