cosenary / Instagram-PHP-API

An easy-to-use PHP Class for accessing Instagram's API.
http://cosenary.github.com/Instagram-PHP-API
BSD 3-Clause "New" or "Revised" License
1.46k stars 782 forks source link

The access_token provided is invalid #235

Closed aghayeff closed 7 years ago

aghayeff commented 7 years ago

I successfully can get the media by tags with my access_token using link at below:

(https://api.instagram.com/v1/tags/qe/media/recent?access_token=MY_TOKEN)

But, while using this api, i receive an error: The access_token provided is invalid

Here is my code:

$instagram = new Instagram($client_id); //
$instagram->setAccessToken($my_token);
$tags = $instagram->getTag("qe");

What is wrong in my code?

gabrielcantarin commented 7 years ago

I'm with same issue :/

angelogulina commented 7 years ago

Same as well.

bado57 commented 7 years ago

Same as well.

dharkness commented 7 years ago

I get the same error while trying to access public APIs like getPopularMedia() and getUser($id). I am not setting an access token because it's not necessary, and I don't have one.

$gram = new Instagram('CLIENT_ID');
var_dump($gram->getPopularMedia());
samdoidge commented 7 years ago

Same.. getUserLikesworks fine, searchTags() displays:

The access_token provided is invalid

Krisseck commented 7 years ago

I think Instagram has changed the API and access tokens are necessary even for public endpoints.

If you provide the library a client_id and access_token and use something that doesn't require authentication, like getUser(), then it uses the client_id in the request. There should be a way to force the library to use access_token if it has supplied, since that provides more information from the API in all cases.

Super quick hack I did was in function getUser(), I set the $auth = true;

macint0sh commented 7 years ago

@Krisseck you absolutely right! Thanks! According to Instagram Platform(API) changelog from Nov 17, 2015 we can see...

...

Apps created on or after Nov 17, 2015

  • Start in Sandbox mode for dev & test
  • Need to be reviewed and permission scopes approved before used in production
  • All API endpoints require a valid access_token

...

So if you want to use this library for your application, you need to do small hack that @Krisseck described above. Because in library code access_token used not in every case(just look in 👇 code)...

...

protected function _makeCall($function, $auth = false, $params = null, $method = 'GET')
{
    if (!$auth) {
        // if the call doesn't requires authentication
        $authMethod = '?client_id=' . $this->getApiKey();
    } else {
        // if the call needs an authenticated user
        if (!isset($this->_accesstoken)) {
            throw new InstagramException("Error: _makeCall() | $function - This method requires an authenticated users access token.");
        }

        $authMethod = '?access_token=' . $this->getAccessToken();
    }

...

As for Me... I prefer to make changes in _makeCall method and change...

$auth=false

on

$auth=true

After this small change every endpoint request uses acces_token! Of course if you setup access_token 😄

This is not good practice! But this is choice if author of the library does not support it and doesn't approve(for a long time) any PR