Search code examples
phpnode.jsinstagraminstagram-api

How to get the media ID using Instagram's API


I've been trying to retrieve the users who have commented on a picture at Instagram. The issue I'm facing is that you require the media ID of the image to use in the api endpoint. I'm using this code to get the media ID of the image:

$api = file_get_contents("http://api.instagram.com/oembed?url=https://www.instagram.com/p/BBHOUQyuS3T/?taken-by=kimkardashian");
$apiObj = json_decode($api,true);
$media_id = $apiObj['media_id'];
print_r($media_id);

which gives me a media ID of 1172969193024859603_18428658.

When I input this into my api request like this:

$api = file_get_contents("http://api.instagram.com/v1/media/".$media_id."/comments?access_token=".$access_token);

with my particular access_token, I get a 400 error code. When I go direct to the page url, it says:

Invalid media id.

Has anyone else had this problem before? If so how did you solve it?

Another puzzling part of this is that, after looking around the Stackoverflow for a while, I found a couple ways of translating the shortcode for the picture into the media ID.

One of them is nodejs module here which gives me a media ID for the image of 1172969193024859603, so like the original but without the bit after the underscore. Both ID's give me the same error.

I've been at this for hours and it's driving me mad! Please help.


Solution

  • Thats the expected response in sandbox mode if you are trying to access a user's media that is not in your sandbox authorized user list.

    https://www.instagram.com/developer/sandbox/

    Data is restricted to sandbox users and the 20 most recent media from each sandbox user

    Add the user who's photo u want to access to your sandbox users, then you will get a valid API response for media

    The media_id 1172969193024859603_18428658 and 1172969193024859603 are the same the part after underscore is the media owner's user_id, both are valid for media_id