Sample code
Sample code for authentication workflows are available in the following languages :
NodeJS
Client credentials Flow
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 consumerId = "YOUR_CONSUMER_ID";
const consumerSecret = "YOUR_CONSUMER_SECRET";
(async () => {
// get an access token
const response = await axios({
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
"Authorization": `Basic ${Buffer.from(consumerId + ":" + consumerSecret).toString('base64')}`
},
data: "grant_type=client_credentials",
url: 'https://oauth2.sandbox.bouyguestelecom.fr/ap4/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)
})()
Authorization code Flow
The code below starts an expressjs http server on port 3000. When the user go to http://localhost:3000, it's redirected to Bouygues Telecom login.
After login, user is redirected to http://localhost:3000/callback, where the code
is received and can be exchanged for an access token using the token endpoint. After that, the access token is used to make an API call.
const axios = require("axios");
const express = require('express')
const consumerId = "YOUR_CONSUMER_ID";
const consumerSecret = "YOUR_CONSUMER_SECRET";
const app = express()
const port = 3000
const redirectUri = `http://localhost:${port}/callback`;
app.get('/', (_, res) => {
res.redirect(`https:///oauth2.sandbox.bouyguestelecom.fr/ap4/authorize?client_id=${consumerId}&response_type=code&redirect_uri=${redirectUri}`)
})
app.get('/callback', async (req, res) => {
if (!req.query.code) {
return res.status(400).json({ error: "no code received" })
}
const response = await axios({
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
"Authorization": `Basic ${Buffer.from(consumerId + ":" + consumerSecret).toString('base64')}`
},
data: `grant_type=authorization_code&code=${req.query.code}&redirect_uri=${encodeURIComponent(redirectUri)}`,
url: 'https:///oauth2.sandbox.bouyguestelecom.fr/ap4/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.sandbox.bouyguestelecom.fr/ap4/userinfo'
})
res.json(result.data);
})
app.listen(port, () => {
console.log(`server listening at http://localhost:${port}`)
})
PHP
Client credentials Flow
The code below get an access token using the client credentials flow and make an API call using that access token
<html>
<?php
$consumerId = "YOUR_CONSUMER_ID";
$consumerSecret = "YOUR_CONSUMER_SECRET";
$options = array(
'http' => array(
'header' =>
"Content-type: application/x-www-form-urlencoded\r\n" .
"Authorization: Basic " . base64_encode($consumerId . ":" . $consumerSecret) . "\r\n",
'method' => 'POST',
'content' => http_build_query(array('grant_type' => 'client_credentials'))
)
);
$context = stream_context_create($options);
$authResult = file_get_contents("https://oauth2.sandbox.bouyguestelecom.fr/ap4/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.sandbox.bouyguestelecom.fr/ap4/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>
Authorization code Flow
The code below suppose you're running an http server on port 3000, like php -S localhost:3000
When the user go to http://localhost:3000, it's redirected to Bouygues Telecom login. After login, user is redirected to http://localhost:3000 where the code
is received and can be exchanged for an access token using the token endpoint. After that, the access token is used to make an API call.
<html>
<?php
$consumerId = "YOUR_CONSUMER_ID";
$consumerSecret = "YOUR_CONSUMER_SECRET";
$redirectUri = "http://localhost:3000";
if (isset($_GET['code']) && isset($_GET['token_type'])) {
$options = array(
'http' => array(
'header' =>
"Content-type: application/x-www-form-urlencoded\r\n" .
"Authorization: Basic " . base64_encode($consumerId . ":" . $consumerSecret) . "\r\n",
'method' => 'POST',
'content' => http_build_query(array(
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => $redirectUri
))
)
);
$context = stream_context_create($options);
$authResult = file_get_contents("https:///oauth2.sandbox.bouyguestelecom.fr/ap4/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.sandbox.bouyguestelecom.fr/ap4/userinfo", false, $context);
// var_dump($apiResult);
} else {
//redirect to login page
header("Location: https:///oauth2.sandbox.bouyguestelecom.fr/ap4/authorize?client_id=" . $consumerId . "&response_type=code&redirect_uri=" . $redirectUri);
die();
}
?>
<body>
<div>
<h2>Authentication result</h2>
<p><?=$authResult?></p>
</div>
<div>
<h2>API result</h2>
<p><?=$apiResult?></p>
</div>
</body>
</html>