negative0
negative0

Reputation: 459

Instagram Authentication using PHP

Hello fellow developers,

I searched everywhere and it seems like this question remain unresolved, some appears to solve this by using proxy servers or changing their IP address, which didn't work in my case. Since Instagram doesn't have an official PHP or JavaScript SDK, I need to ask this question here.

When I attempt to make a Oauth Call to Instagram by retrieved token it will return an error message below:

{"code": 400, "error_type": "OAuthException", "error_message": "Matching code was not found or was already used."}

I have included by code below for your observation:

Getting Token

private function getLoginURL($scopes = array("basic")) {
    return self::API_OAUTH_URL."/?client_id=".self::CLIENT_ID."&redirect_uri=".self::REDIRECT_URI."&scope=".implode("+", $scopes)."&response_type=code";
}

Authentication (Oauth 2.0)

private function auth() {
    $api_data = array(
        'client_id'     => self::CLIENT_ID,
        'client_secret' => self::CLIENT_SECRET,
        'grant_type'    => 'authorization_code',
        'redirect_uri'  => urlencode(self::TOKEN_URL),
        'code'          => self::TOKEN
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, self::TOKEN_URL);
    curl_setopt($ch, CURLOPT_POST, count($api_data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($api_data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 90);

    $json_data = curl_exec($ch);
    curl_close($ch);
    //return json_decode($json_data, true);
    return $json_data;
}

Could someone help me out :(

Upvotes: 0

Views: 1337

Answers (1)

Queue
Queue

Reputation: 456

Problem: Your response type is code.

  1. Due to new changes in Instagram's API, you will need your response type to be token.
  2. This may require some reworking of your existing code.

  3. While using token, the scope specification comes afterwards.

    private function getLoginURL($scopes = array("basic")) {
        return self::API_OAUTH_URL."/?client_id=".self::CLIENT_ID."&redirect_uri=".self::REDIRECT_URI."&response_type=token&scope=".implode("+", $scopes);
    }
    

Upvotes: 1

Related Questions