Progziner
Progziner

Reputation: 43

Getting error 'No authorization header passed' with PHP's POST CURL request - envato api

I'm getting started with the Enavato API

So far I've created an app, got client_id & client_secret and managed to get the code access_key from the https://api.envato.com/authorization after that I'm using the below php code to make POST curl request

$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;

and here is the request.txt dump

* 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

and finally the error(getting it in JSON)

string(80) "{"error":"invalid_request","error_description":"No authorization header passed"}"

Astonishingly every thing is working with Postman and I'm getting "refresh_token" and "access_token" with a success 200 status code.

I know I'm missing some thing but couldn't find what?

Upvotes: 4

Views: 1802

Answers (1)

Hans Z.
Hans Z.

Reputation: 53928

You're using curl_setopt($curl, CURLOPT_POSTFIELDS, $params); with $params being an array. That results in a HTTP POST message with multipart/form-data content type and formatting. But the spec says the content type should be application/x-www-form-urlencoded. You can achieve this by using:

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

You also don't need to urlencode the redirect_uri parameter, since http_build_query will do it for you.

Lastly, do not turn off SSL validation (CURLOPT_SSL_VERIFYHOST, CURLOPT_SSL_VERIFYPEER) since it renders your system insecure.

Upvotes: 3

Related Questions