为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
隔江千里 2022-12-23 16:35:37
我一直在尝试:$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?还是我用错了?
查看完整描述

1 回答

?
浮云间

TA贡献1829条经验 获得超4个赞

从此链接找到答案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"

  ),

));


查看完整回答
反对 回复 2022-12-23
  • 1 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信