dongnigeng1295 2018-10-11 11:57
浏览 196
已采纳

在PHP页面上使用带有CURL的OAUTH API

I'm working with a PHP website where I want to integrate to some 3rd party API.

In particular I am looking at using CURL to interact with their server but this is something I am far from an expert in so hoping the community can help me gain a better understanding of what I am doing.

I am unclear what options such as -X and -d do, also I am unclear how I script this command on my PHP page? (Unfortunately it's tricky searching google for "-d" as this isn't considered part of the search string)

My particular example I am stuck on is requesting an access token, the API documentation provided to me is;

curl -X POST \
-d \ "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&scope=REQUESTED_SCOPES" \
'https://api.example.com/token'

grant_type- client_credentials

client_id- Generated during setup

client_secret - Web apps only, generated during setup

scope Optional - List of comma separated values, see supported scopes

If the request is successful, an access token will be returned in the response:

 {
"access_token":"ACCESS_TOKEN",
"token_type":"Bearer",
"expires_in":3600
"scope":"REQUEST_SCOPES"
}

That is the above guidance, I have completed the pre-requisites so can confirm the client id, secret and required scope are correct.

I have tried both of the following in vein in my PHP script

$tk = curl_init();

curl_setopt($tk, CURLOPT_URL, "https://api.example.com/token");
curl_setopt($tk, CURLOPT_POST, 1);
curl_setopt($tk, CURL_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($tk, CURLOPT_POSTFIELDS, array( 'grant_type=client_credentials&client_id=myownid&client_secret=xyz123&scope=instrument'));

// grab URL and pass it to the browser
$result=curl_exec($tk);

// close cURL resource, and free up system resources
curl_close($tk);

And

$tk = curl_init();

curl_setopt($tk, CURLOPT_URL, "https://api.example.com/token?grant_type=client_credentials&client_id=myownid&client_secret=xyz123&scope=instrument");
curl_setopt($tk, CURLOPT_POST, 1);
curl_setopt($tk, CURL_HTTPHEADER, array('Content-Type: application/json'));

// grab URL and pass it to the browser
$result=curl_exec($tk);

// close cURL resource, and free up system resources
curl_close($tk);

Both of these examples produce the following error;

{"error_description":"grant_type parameter is missing","error":"invalid_request"}

Any help on this particular issue or even to just understand how I am going wrong to give me some ideas of the correct syntax will be much appreciated!

Thank you all in advanced for your time.

  • (Please note I've changed to "example.com" for security of the 3rd party)
  • 写回答

2条回答 默认 最新

  • dongqun1656 2018-10-11 12:17
    关注

    Check out below sample code of cURL call in php. You need to change your domain name instead of example.com also put values for POSTFIELDS.

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.example.com/oauth/token",
      CURLOPT_RETURNTRANSFER => true,
      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\",\"scope\": \"instrument\"}",
      CURLOPT_HTTPHEADER => array(
        "content-type: application/json"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验