Error in Restful API authentication with Curl PHP

Asked

Viewed 825 times

0

I’m writing a web platform that uses Sptrans RESTFUL API - Living 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;

    ?>

Who returns to me

True

When I try to use the API elsewhere in the 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
    ?>

returning

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

What am I doing wrong?

Thank you

  • Voce checked if you need to pass some webtoken? And the so-called method accepts get request?

  • @tkmattos sim precisa. The method is called via POST /Login/Authenticate? token={token} and the second is GET

2 answers

0

Hello, to check this endpoint, according to the documentation, you need to make the call using the GET method, and also, do it by passing the token per parameter, as you did earlier.

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'

0

I got the sponse of post authentication

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

either with the name or with the position of the header of sponse (login success 200), I caught the cookie that I received

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

passed as cookie header. in the requestGET and created JSON with the 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.