如何使用客户端凭据作为 grant_type 的 PHP curl 获取 OAuth 2.0?

我一直在尝试:


$header = array(

                "Authorization :Basic $authorization",

                "Content-Type: application/json"

                );


//option 1

$param='{

    "grant_type" : "client_credentials"

        }';


//option 2

$param = array("Grant_type: client_credentials",);


//option 3

$param = "grant_type=client_credentials";


//option 4

$a = array( 

     'grant_type' => 'client_credentials');

$param = http_build_query($a);


$ch = curl_init( $url );          

curl_setopt($ch,CURLOPT_POST,1);             

curl_setopt($ch,CURLOPT_POSTFIELDS,$param);

curl_setopt($ch,CURLOPT_VERBOSE, true);   

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);    

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,120);     

curl_setopt($ch,CURLOPT_TIMEOUT,120);  

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch,CURLOPT_HTTPHEADER,$header);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);

$content = curl_exec( $ch ); 

$err = curl_error($ch);        

curl_close( $ch );

我已经尝试了上面的所有选项,但不断收到这样的错误:


response_type or grant_type is required

如何在 curl 概念中传递 grant_type?还是我用错了?


隔江千里
浏览 98回答 1
1回答

浮云间

从此链接找到答案https://auth0.com/docs/api-auth/tutorials/client-credentials需要添加以下行:CURLOPT_SSL_VERIFYHOST => 假,CURLOPT_SSL_VERIFYPEER => 假,$curl = curl_init();curl_setopt_array($curl, array(  CURLOPT_URL => "https://xxy",  CURLOPT_RETURNTRANSFER => true,  CURLOPT_SSL_VERIFYHOST =>false,  CURLOPT_SSL_VERIFYPEER => false,  CURLOPT_ENCODING => "",  CURLOPT_MAXREDIRS => 10,  CURLOPT_TIMEOUT => 30,  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,  CURLOPT_CUSTOMREQUEST => "POST",  CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET",  CURLOPT_HTTPHEADER => array(    "content-type: application/x-www-form-urlencoded"  ),));
打开App,查看更多内容
随时随地看视频慕课网APP