如何在PHP中执行oauth2.0令牌请求

问题描述 投票:0回答:1

我试图在 php 中使用 cURL 请求 oauth 令牌,但响应始终是 {"error":"invalid_request"}

我的代码:

 $client_id = 'MY_CLIENT_ID';
 $client_secret = 'MY_CLIENT_SECRET';
 $url = my url '/identityiq/oauth2/token';

 
 
$requestData = [
    'grant_type' => 'client_credentials',
    'client_id' => $client_id,
    'client_secret' => $client_secret
];

   
$ch = curl_init();

$headers['Content-Type'] = "application/x-www-form-urlencoded";


curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
var_dump($response);

curl_close($ch);

我尝试了邮递员授权,它正在工作

php oauth-2.0
1个回答
0
投票

解决了! 问题是 client_id 和 client_secret 需要在调用的标头中传递并解码为 base64。

$headers['授权'] = "基本" 。 base64_encode('CLIENT_ID:CLIENT_SECRET');

© www.soinside.com 2019 - 2024. All rights reserved.