使用PHP的POST CURL请求获取错误'没有授权标头' - envato api

时间:2015-05-17 17:20:20

标签: php curl oauth-2.0

我开始使用Enavato API

到目前为止,我已经创建了一个应用,获得了client_id& client_secret并设法从code获取https://api.envato.com/authorization access_key,之后我使用下面的php代码发出POST卷曲请求

$client_id      = '***********';
$client_secret  = '***********';
$redirect_uri   = urlencode('http://localhost:3000');

if(isset($_GET["code"])) :  

  $apiUrl = 'https://api.envato.com/token';
  $params = array(
    'grant_type'    => 'authorization_code',
    'code'          => $_GET["code"],
    'redirect_uri'  => $redirect_uri,
    'client_id'     => $client_id,
    'client_secret' => $client_secret,
  );

  $curl = curl_init();
  $f = fopen('request.txt', 'w');
  curl_setopt($curl, CURLOPT_URL, $apiUrl);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_VERBOSE, true);
  curl_setopt($curl, CURLOPT_HEADER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  curl_setopt($curl, CURLOPT_STDERR, $f);

  $result = curl_exec($curl);
  fclose($f);

  // Check if any error occurred
  if(empty($result))
  { 
    // die(curl_errno($curl));
    // die(curl_error($curl));
     $info = curl_getinfo($curl);

     echo '<br><br>';
     echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
     echo '<br><br>';
     var_dump($info);
     echo '<br><br>';
  }

  var_dump($result);

  // Close handle
  curl_close($curl);

endif;

这里是request.txt转储

* Hostname was found in DNS cache
* Hostname in DNS cache was stale, zapped
*   Trying 107.23.230.180...
* Connected to api.envato.com (107.23.230.180) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: *.envato.com
* Server certificate: RapidSSL SHA256 CA - G3
* Server certificate: GeoTrust Global CA
> POST /token HTTP/1.1
Host: api.envato.com
Accept: */*
Content-Length: 699
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------1a95a06d7b815306

< HTTP/1.1 100 Continue
< HTTP/1.1 400 Bad Request
< Access-Control-Allow-Origin: *
< Cache-Control: no-store
< Content-Type: application/json; charset=utf-8
< Date: Sun, 17 May 2015 17:03:41 GMT
< Pragma: no-cache
* Server nginx/1.7.10 is not blacklisted
< Server: nginx/1.7.10
< set-cookie: connect.sid=s%3ARC9gGye-Txp4KLp67M9ESspXijYoUc8i.pT3jYHvu1WyOsSjwsuQzEsy5hLQlc2QpmHkZRm05pXo; Path=/; HttpOnly
< X-Frame-Options: Deny
< X-Powered-By: Express
< Content-Length: 80
< Connection: keep-alive
* HTTP error before end of send, stop sending
< 
* Closing connection 0

最后是错误(以JSON获取)

  

string(80)“{”error“:”invalid_request“,”error_description“:”没有通过授权标题“}”

令人惊讶的是,每件事都与Postman合作,我获得了“refresh_token”和“access_token”,并获得了成功的200状态代码。

我知道我错过了一些但找不到的东西?

1 个答案:

答案 0 :(得分:3)

您正在使用curl_setopt($curl, CURLOPT_POSTFIELDS, $params);$params是一个数组。这会导致HTTP POST消息具有multipart/form-data内容类型和格式。但规范说内容类型应为application/x-www-form-urlencoded。您可以使用以下方法实现此目的:

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));

您也不需要urlencode redirect_uri参数,因为http_build_query会为您执行此操作。

最后,请勿关闭SSL验证(CURLOPT_SSL_VERIFYHOSTCURLOPT_SSL_VERIFYPEER),因为它会导致您的系统不安全。