2
votes

Try to get Access Token From Instagram

       $params = array(
            'app_id' => $this->clientId,
            'app_secret' => $this->clientSecret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $this->redirectUrl,
            'code' => $this->request->code
        );
        $ch = curl_init();
        $endpoint = $this->getBaseUrl() . 'oauth/access_token';
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $endpoint);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 1);


        $response = curl_exec($ch);

        curl_close($ch);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($response, 0, $header_size);
        $body = substr($response, $header_size);
        dd($response,$header_size,$header,$body);
        $response = json_decode($response, true);
        return $response;

this code with CURL working fine.

Request Data Laravel Documentation.

$response = Http::post($this->getBaseUrl() . 'oauth/access_token', [
            'app_id' => $this->clientId,
            'app_secret' => $this->clientSecret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $this->redirectUrl,
            'code' => $this->request->code
        ]
    );
   return $response->body();

but not working with Laravel Http Client.it return Client Id is missing but I'm sending as parameter.

1
It's a bit unclear what you are trying to achieve. It seems almost that you are trying to access your own OAuth every point, correct? Anyway, shouldn't you be sending client_id instead of app_id, as described here: laravel.com/docs/8.x/passport#requesting-tokensMaarten Veerman
what is the content-type of the requestbhucho
@MaartenVeerman to get Access Token From Instagram.Fredericka Hartman

1 Answers

3
votes

I guess your content type is application/x-www-form-urlencoded, try to use asForm(),

$response = Http::asForm()->post($this->getBaseUrl() . 'oauth/access_token', [
            'app_id' => $this->clientId,
            'app_secret' => $this->clientSecret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $this->redirectUrl,
            'code' => $this->request->code
        ]
    );
return $response->json();