Sunday, May 6, 2018

Unauthenticated error when accessing data from external API (quandl.com) if not logged in - development

Unauthenticated error when accessing data from external API (quandl.com) if not logged in

I have this code in my controller:

            $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://www.quandl.com/api/v3/datasets/WIKI/AVP.json?rows=1&api_key=dEGK9fT3NU**********",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_TIMEOUT => 30000,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
            ),
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);

        if ($err) {
            return response(['message' => 'Oops!, Something went wrong.'], 500);     
        } else {
            $result = json_decode($response);
            return (array) $result;
        }

and this in my web.php:

Route::get('/get-stocks/{symbol}', 'HomeController@getStocks');

the route is placed outside the middleware so I assumed that it should be able to make the request and return the correct output regardless if a user is logged in or not. Unfortunately, if no user is logged in, the request only returns

 { error: unauthenticated. } 

response. Can someone please explain to me why this is happening, and what possible solution(s) I have? Thanks!



from Laravel Questions and Answers https://laravelquestions.com/php/unauthenticated-error-when-accessing-data-from-external-api-quandl-com-if-not-logged-in/
via Lzo Media

No comments:

Post a Comment