简体   繁体   中英

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

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 :)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM