Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I follow users using the instagram API without the limit rate issue?

Tags:

php

api

instagram

I want to follow a list of user ids in instagram. I made the list and when I try to send the follow request using the api, I get this error: Client request limit reached (code: 400)

The developer documentation says: "You are limited to 5000 requests per hour per access_token or client_id overall."

but when I get this error, my code hardly followed 50 user ids!

And one more thing, when I got this error I tried to follow people on the instagram app directy (not using the api) and there were no issues!

This is some part of my code that get's this error:

foreach ($usersArr as $user) {
$i++;
$url = 'https://api.instagram.com/v1/users/'.$user->id.'/relationship?access_token='. $accessToken;
$data = 'action=follow';
$resultObj = json_decode(sendPostData($url, $data));
$responseCode = $resultObj->meta->code;
$errorMsg = $resultObj->meta->error_message;
if ($responseCode != 200)
    echo "\"" . $errorMsg . "\" AT " . date('Y-m-d H:i:s') . "\n\n";
while ($responseCode != 200){
    sleep(60);
    $resultObj = json_decode(sendPostData($url, $data));
    $responseCode = $resultObj->meta->code;
    if ($responseCode != 200) {
        $errorMsg = $resultObj->meta->error_message;
        echo "STILL \"" . $errorMsg . "\" AT " . date('Y-m-d H:i:s') . "\n\n";
    }
    elseif($responseCode == 200){
        echo "Limit Finished At: " . date('Y-m-d H:i:s') . "\n\n";
        break;
    }
}
echo "User NO. " .$i . " has been followed!" . "\n\n";
}

Thanks a lot :)

like image 227
Sepehrooo Avatar asked Jan 27 '14 23:01

Sepehrooo


People also ask

Does Instagram limit follow requests?

Anyone who tries to follow more than 7,500 people will see an error message, regardless of how many followers they have. Anyone who's currently following more than 7,500 people did so before this change was made. Learn how to unfollow someone on Instagram. Learn more about our Community Guidelines.

Can you have more than 1000 follow requests on Instagram?

Yes, you can have a million request pending on your private instagram account . But the fact is that it will show only 1000 requests pending indeed you are having more more request pending .


2 Answers

My experience with the relationship endpoint of the API is that you are limited to around 160 requests per hour. This appears to be shared with all the actions (follow/unfollow/block/unblock/approve/deny). I have been unable to find anything that allows you to determine in advance how many requests you have left before you exceed your limit (unlike the X-Ratelimit-Remaining header in for the overall request limit), and I have noticed that a any attempt to use the endpoint is added to your request count, whether or not it succeeds.

Because my particular app is processing for multiple people concurrently, I process in small chunks (around 50 requests) and when I finish a chunk, or if I hit a rate limit within the chunk, I move on to another user for a while (typically around 10-15 minutes) before coming back.

I imagine the API limit is per-appid/clientkey, which is why you could follow users on the instagram app despite reaching your limit.

like image 148
mykel Avatar answered Sep 23 '22 21:09

mykel


Note, the API is complaining about requests, not follows. You may be following 50 users but you have made API requests that exceed 5000. With your current code, it will only take 100 page refreshes for you to reach that limit. Might sound like a lot of refreshes but if you are debugging/developing code then it is plausible to assume that you could exceed the 100 refresh threshold with relative ease.

To mitigate this problem, if you are still working/debugging this code then reduce the number of follows from 50 to 1 or 2. Once your code is working then reinstate the previous number of follows. I would recommend you cache the return objects too, for 5 or 10 minutes. That way you won't need to worry about exceeding the API limits.

like image 34
Rijndael Avatar answered Sep 21 '22 21:09

Rijndael