Как решить проблему отсутствия значения параметра grant_type в API с помощью Laravel и cURL - PullRequest
0 голосов
/ 03 февраля 2019

Я создаю веб-сайт для просмотра результатов, который дает мне API.Итак, сначала я сгенерировал токен доступа из Почтальона, а затем включил его в заголовок API.Затем я попытался сгенерировать токен доступа с помощью cURL.Но это всегда дает мне следующую ошибку.

{# 480 ▼ + "error_description": "Отсутствует значение параметра grant_type"
+ "error": "invalid_request"}

Как я могу решить эту проблему?

Вот контроллер, который я использовал для генерации токена доступа.(AccessToken.php)

public function authticate(){

        $tokengen = "base64(clientid:clientsecret)";

        $url = 'http://api.gate.com.:8254/token?=';
        $tokengenheaders = array(
            'grant_type: client_credential',
            'Content-Type: application/x-www-form-urlencoded',
            'Authorization: Basic '.$tokengen

        );

        $ch2 = curl_init();
        curl_setopt($ch2, CURLOPT_URL, $url);
        curl_setopt($ch2, CURLOPT_HTTPHEADER, $tokengenheaders);
        curl_setopt($ch2, CURLOPT_POST, true);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);

        $tokenid = curl_exec($ch2);
        curl_close($ch2);

        return $tokenid;

    }

Здесь находится контроллер с API. (SLAPIControoler.php)

public function SearchSchedule(Request $request)
    {
        $StartCity = $request->input('StartCity');
        $EndCity = $request->input('EndCity');
        $StartDate = $request->input('StartDate');
        $StartTime = $request->input('StartTime');
        $EndTime = $request->input('EndTime');
        $Lang = 'en';

        $auth = new AccessToken();
        $authtoken = json_decode($auth->authticate());
//        dd($authtoken); die();

        $url = "http://api.gate.com:8253/bus/1.0/searchTrain?startStationID=".$StartCity."&endStationID=".$EndCity."&searchDate=".$StartDate."&startTime=".$StartTime."&endTime=".$EndTime."&lang=".$Lang;

        $headers2 = array(
            'Authorization: Bearer ' . $authtoken,
            'Content-Type: application/json'
        );

        $ch2 = curl_init();
        curl_setopt($ch2, CURLOPT_URL, $url);
        curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers2);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode($decodedjsonarray));
        // echo(curl_exec($ch2));

        $results = json_decode(curl_exec($ch2));
        curl_close($ch2);

        dd($results); die();

        //$GetCities = DB::table('airports')->get();
        //$resultsets = array("data" => $results, "totaltravlers" => $AllThePassengers, "faredetails" => $revalidationarray, "GetCities" => $GetCities);

        //return view('Metro.reviewpage')->with('resultsets', $resultsets);

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...