首页 > 解决方案 > Laravel HTTP 客户端检索 REST API 访问令牌

问题描述

尝试从 MS Azure 检索访问令牌

像这样的东西:

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Response;

class HttpController extends Controller
{
    public function index()
    {
        $url = "https://login.microsoftonline.com/[tenantId]/oauth2/token";

        $response = HTTP::post($url,
        [
            'grant_type' => 'client_credentials',
            'client_Id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
            'client_secret' => '***************************',
            'resource' => 'https://management.azure.com',
        ]);

         dd($response);
    }
}

得到以下错误:

“error”:“invalid_request”,“error_description”:“AADSTS900144:请求正文必须包含以下参数:'grant_type'

标签: authenticationaccess-tokenguzzlelaravel-7

解决方案


$response = HTTP::asForm()->post($url,
        [
            'grant_type' => 'client_credentials',
            'client_Id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
            'client_secret' => '***************************',
            'resource' => 'https://management.azure.com',
            ]
        );

推荐阅读