fork download
  1. <?php
  2. // Parámetros de configuración
  3. $tenantId = "4d0d72ca-4111-4d09-80be-d91acd4612a7"; // Tu tenant ID
  4. $clientId = "beab2a03-ee5b-4847-9844-93a183959c6e"; // El Client ID de la app
  5. $clientSecret = "5x88Q~T2LWT2CSifzct~nWCYC2tZX9ZbHWP4CcnJ"; // El secreto generado en "Certificates & secrets"
  6. $scope = "api://beab2a03-ee5b-4847-9844-93a183959c6e/.default"; // Ajusta con el ID URI de tu API
  7.  
  8. // Endpoint de token de Azure AD (v2.0)
  9. $url = "https://l...content-available-to-author-only...e.com/{$tenantId}/oauth2/v2.0/token";
  10.  
  11. // Los datos que enviaremos en el POST
  12. $data = [
  13. 'client_id' => $clientId,
  14. 'client_secret' => $clientSecret,
  15. 'scope' => $scope, // Para client_credentials, usar .default
  16. 'grant_type' => 'client_credentials'
  17. ];
  18.  
  19. // Iniciamos cURL
  20. $ch = curl_init($url);
  21. curl_setopt($ch, CURLOPT_POST, true);
  22. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  24.  
  25. // Ejecutamos la petición
  26. $response = curl_exec($ch);
  27. if ($response === false) {
  28. die('Error en cURL: ' . curl_error($ch));
  29. }
  30. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  31.  
  32. // Verificamos código HTTP
  33. if ($httpCode >= 200 && $httpCode < 300) {
  34. // Decodificamos el JSON
  35. $json = json_decode($response, true);
  36. if (isset($json['access_token'])) {
  37. $accessToken = $json['access_token'];
  38. echo "Access Token obtenido exitosamente:\n";
  39. echo $accessToken;
  40. } else {
  41. echo "Error: No se encontró 'access_token' en la respuesta.\n";
  42. print_r($json);
  43. }
  44. } else {
  45. echo "La solicitud falló. Código HTTP: $httpCode\n";
  46. echo "Respuesta: $response\n";
  47. }
  48. ?>
  49.  
Success #stdin #stdout 0.03s 26484KB
stdin
Standard input is empty
stdout
Error en cURL: Could not resolve host: login.microsoftonline.com