2

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.

3
  • 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-tokens Commented Dec 9, 2020 at 15:41
  • what is the content-type of the request Commented Dec 9, 2020 at 15:56
  • @MaartenVeerman to get Access Token From Instagram. Commented Dec 9, 2020 at 15:56

1 Answer 1

6

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Yep this did it for me, Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.