jac
jac

Reputation: 43

Instagram Basic Display API returning error

I am trying to request an access token from Instagram's Basic Display API but it is giving me this error:

{"error_type": "OAuthException", "code": 400, "error_message": "Missing required field client_id"}

This is odd because I am providing a client_id. Here is my code:

$client_id      = 'xxx';
$client_secret  = 'xxx';
$redirect_uri   = 'xxx';

if (isset($_GET['code'])) {

    $code = $_GET['code'];

    echo $code;

    $url = 'https://api.instagram.com/oauth/access_token';
    $data = array([
                'client_id'     => $client_id,
                'client_secret' => $client_secret,
                'grant_type'    => 'authorization_code',
                'redirect_uri'  => $redirect_uri,
                'code'          => $code
            ]);

    $curl = curl_init($url);
    curl_setopt($curl,CURLOPT_POST,true);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);   // to stop cURL from verifying the peer's certificate.
    $result = curl_exec($curl);
    curl_close($curl);

    echo $result;

}else{
    echo '<a href="https://api.instagram.com/oauth/authorize?client_id='.$client_id.'&redirect_uri='.$redirect_uri.'&scope=user_profile,user_media&response_type=code">connect your instagram</a>';
}

Edit:

When I run this test in Postman, I get the same error if I put the data in the params but if I put it in body / x-www-form-urlencoded it works. This is a definite clue to why it isn't working...

What am I doing wrong?

Upvotes: 2

Views: 857

Answers (1)

tony
tony

Reputation: 610

Change:

$data = array([
            'client_id'     => $client_id,
            'client_secret' => $client_secret,
            'grant_type'    => 'authorization_code',
            'redirect_uri'  => $redirect_uri,
            'code'          => $code
        ]);

To:

$data = array(
            'client_id'     => $client_id,
            'client_secret' => $client_secret,
            'grant_type'    => 'authorization_code',
            'redirect_uri'  => $redirect_uri,
            'code'          => $code
        );

And it will work.

Upvotes: 1

Related Questions