Restful API authentication error with PHP cURL

0

I am writing a web platform that uses the API RESTFUL of SPTrans - Live Eye . Authenticate my session with the code:

<?php  
      $url = 'http://api.olhovivo.sptrans.com.br/v2.1/Login/Autenticar?token='.token;
      $curl = curl_init($url);


      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS, "");

      $result = curl_exec($curl);

      curl_close($curl);

      echo $result;

    ?>

That returns me

  

True

When I try to use the API in another piece of code, I have authentication problems.

        <?php 
      $url = 'http://api.olhovivo.sptrans.com.br/v2.1/Linha/Buscar?termosBusca=8000';

      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

      $result = curl_exec($curl);
      curl_close($curl);        

      echo $result
    ?>

that returns

  

{"Message": "Authorization has been denied for this request."}

What am I doing wrong?

Thank you

    
asked by anonymous 14.12.2017 / 17:39

2 answers

0

Hello, to query this endpoint, according to the documentation, it is necessary to make the call using the GET method, and in addition, do it by passing the token per parameter, as you did previously.

curl -X GET \ 'http://api.olhovivo.sptrans.com.br/v2.1/Linha/Buscar?termosBusca=8000&token=%3C%20%7C%20O%20SEU%20TOKEN%20VEM%20AQUI%20%7C%20%3E' \ -H 'cache-control: no-cache' \ -H 'postman-token: dd8ecd6b-5b3f-5758-0050-f9db20e6fddc'

    
09.01.2018 / 17:12
0

I got the response from the post authentication

WebResponse response = (HttpWebResponse)Request.GetResponse();

header header header (success 200 login), I got the cookie I received p>

string  SC = response.Headers.Get("Set-Cookie"); 
string posicao = response.Headers.Get(6);

I passed as header.cookie in requestGET and deserealized the JSON with StreamReader

HttpWebRequest RequestGETBusca = (HttpWebRequest)WebRequest.Create(UrlBusca);



        RequestGETBusca.Headers.Add(HttpRequestHeader.Cookie, ck);
            RequestGETBusca.Method = "GET";



            WebResponse responseBusca = (HttpWebResponse)RequestGETBusca.GetResponse();

            StreamReader reader = new StreamReader(RequestGETBusca.GetResponseStream(), System.Text.Encoding.UTF8);
            String resultData = reader.ReadToEnd();
    
30.06.2018 / 07:51