Je vous propose une fonction pour effectuer le OAuth 2.0 authorization Basic et Client Credentials (flux 3 et 4).

Cette méthode repose sur un Client_id et Client_Secret et permet d'obtenir un Token a durée de vie illimitée (pas de Refresh Token) selon la spécification rfc6749.
Pour obtenir ce token (flux 4), des éléments doivent être fournis tels que client_id, client_secret, uri, oauth_url (flux 3).
Cette réquête sera insérée dans le header à la rubrique Authorization, avec d'autres éléments plus classiques d'un header de type Content-Type, grant_type.
Le requête httpQuery() peut alors être envoyée avec les paramètres $url, 'POST', $post = $payload, $oauth_token = NULL, $header, $use_cookies = false, $ignore_errors = false, &$header_received, $user_pwd = NULL).
En réponse on obtient une chaine Json avec le token, le token_type et dans le header de la réponse HTTP 200 OK.
Il y a beaucoup de documentation à ce sujet sur internet, j'ai utilisé principalement celle-ci: https://www.rfc-editor.org/rfc/rfc6749.txt
Le script PHP (compatible eedomus

Il est important de déclarer dans le script principal:
$client_id=xxxxxx;
$client_secret=yyyyyy;
$header_received=array(timeout=>5);
$uri=<votre url du serveur>;
$oauth_url=<le chemin url pour la demande de token>;
on peut ensuite faire appel à la fonction sdk_OAuth2_basic_client_credentials().
- Code : Tout sélectionner
function sdk_OAuth2_basic_client_credentials($host,$oauth_url,$client_id,$client_secret) {
/*
Based on RFC 6749 see https://www.rfc-editor.org/rfc/rfc6749.txt for more details §4.4. Client Credentials Grant
host : REQUIRED. Value is the servor url
basicAuth : REQUIRED. Value is base64_encode(client_id:client_secret)
grant_type: REQUIRED. Value MUST be set to "client_credentials".
Input parameters:
$host : servor uri
$oauth_url : OAuth url
$client_id : developper identification
$client_secret : developper password
Output parameters:
$response : json string containing access_token, token_type
$header_received: header received from the servor including ERROR code and description
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token":"2YotnFZFEjr1zCsicMWpAA",
"token_type":"example",
"expires_in":3600,
"example_parameter":"example_value"
}
*/
global $header_received;
$payload = "grant_type=client_credentials";
$basicAuth = base64_encode(urlencode($client_id).":".urlencode($client_secret));
$header = array("Accept: application/json", "Authorization: Basic ".$basicAuth, "Content-Type: application/x-www-form-urlencoded");
$url = $host.$oauth_url;
$response = httpQuery($url, 'POST', $payload, NULL, $header, FALSE, FALSE, &$header_received);
return $response;
}
Vos commentaires sont la bienvenue pour améliorer cette boîte à outils.
Bonne weekend à toutes et tous.
dommarion