Http Request returns blank page

0

I'm trying to perform Http request in php which should open a web page so that I would look for a code that would come in the URL after authorizing the API. But when I run the page it goes blank. I tried cURL and the result really was.

<?php

$request = new HttpRequest();
$request->setUrl('https://api.contaazul.com/auth/authotize');
$request->setMethod(HTTP_METH_GET);

$request->setQueryData((
    'redirect_uri' => 'http://contaazul.com',
    'cliente_id' => '',
    'scope' => 'sales',
    'state' => 'DCEeFWf45A53sdfKef424'
));

try{
    $response = $request->send();

    echo $responde->getBody();
} catch (HttpException $ex) {
    echo $ex;
}

?>

Could someone give me a light where I might be wrong? I did the authentication for Postman and it worked as expected, the only difference is that there was "Postman-Token" in the header. But I tested with it and still no success.

In cURL it was as follows:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.contaazul.com/auth/authorize?redirect_uri=http://contaazul.com&client_id={CLIENTE_ID}&scope=sales&state=DCEeFWf45A53sdfKef424",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

It also worked on Postman.

    
asked by anonymous 29.03.2018 / 19:17

1 answer

0

Conta Azul uses OAuth2 as a form of authentication. To login you need to redirect the user to the route:

https://api.contaazul.com/auth/authorize?redirect_uri={REDIRECT_URI}&client_id={CLIENT_ID}&scope=sales&state={STATE}

After entering the client login and password it will be redirected to the callback url defined by you at the time of registering your application.

In the callback url you will receive state and code . The state is to avoid CSRF (a string generated by you), code must be sent on the next request to get a real access token at the endpoint:

https://api.contaazul.com/oauth2/token

This endpoint is wrong:

$request->setUrl('https://api.contaazul.com/auth/authotize');

At the end it should be authoRize

Reference:

link

    
29.03.2018 / 22:38