Where do I find the Instagram media ID of a image (having access token and following the image owner)?

我的梦境 提交于 2019-12-22 15:36:14

问题


I have image page url of instagram photo. Now i want to send this image page url to instagram api and obtain media id and owner username. The following api call works well if the image is public but if the image is private it will fail and give me No Media Match.

I am already following the image owner and have access token to user instagram api. So how i can get media id and image owner name having the image page url using php or javascript ? is there any other api i need to user that deals with private images ?

http://api.instagram.com/oembed?url=http://instagram.com/p/xxxx-xxxxx/

回答1:


So there are a lot of depreciated solutions out there, so here are my edits/solutions that are working as of this date.

Javascript + jQuery

$.ajax({
    type: 'GET',
    url: 'http://api.instagram.com/oembed?callback=&url='+Url, //You must define 'Url' for yourself
    cache: false,
    dataType: 'json',
    jsonp: false,
    success: function (data) {
        try {
            var MediaID = data.media_id;
        } catch (err) {
            console.log(err);
        }
   }
});

So this is just an updated version of @George's code, and is currently working. However I made other solutions that will avoid an ajax request:

Shortcode Key Solution

Certain Instagram urls use a shorten url syntax. This allows the client to just use the shortcode in place of the media idea IF requested properly.

An example shortcode url looks like this: https://www.instagram.com/p/Y7GF-5vftL/

The "Y7GF-5vftL" is your shortcode for the picture.

                var Key = "";
                var i = url.indexOf("instagram.com/p/") + 16; //Once again you need to define 'url' yourself.
                for(;i<=url.length-1;i++)//length is invalid but shouldn't matter because it won't make to past 10 chars
                {
                    if (url.charAt(i) == '/')
                        break;
                    Key += url.charAt(i);
                }

In the same scope, 'Key' will contain your shortcode. Now to request lets say, a low res picture, using this shortcode, you would do something like the following:

//check to see if it is a shortcode url or not(optional but recommended)
                if (Key.length <= 12) { //is
                    $.ajax({
                        type: "GET",
                        dataType: "json",
                        jsonp: false,
                        cache: false,
                        url: "https://api.instagram.com/v1/media/shortcode/" + Key + "?access_token=" + access_token, //Define your 'access_token'
                        success: function (RawData) {
                            var LowResURL = RawData.data.images.low_resolution.url;
                        }
                    });
                }

There are lots of other useful information in the returned RawData structure. Log it or look up the api documentation to see.

Full Page Solution So what if you have the URL to a full instagram page like: https://www.instagram.com/p/BAYYJBwi0Tssh605CJP2bmSuRpm_Jt7V_S8q9A0/

Well you can actually read the HTML to find a meta property that contains the Media ID. There are also a couple other algorithms you can preform on the URL itself to get it, but I believe that requires too much effort so we will keep it simple.

NOTE: This is a little unstable and is vulnerable to being patched. This method does NOT work on a page that uses a preview box. So if you give it the current HTML when you click on a picture in someone's profile, this WILL break and return a bad Media ID.

var MediaID = "";
var e = HTML_String.indexOf("al:ios:url") + 42; //HTML_String is a variable that contains all of the HTML code for the URL. There are many different ways to retrieve this so look one up. You do need to define this.
for (var i = e; i <= e + 100; i++) {//100 should never come close to being reached
    if (request.source.charAt(i) == "\"")
       break;
    MediaID += request.source.charAt(i);
}

And there you go, a bunch of different ways to use Instagram's api to get a Media ID. Hope one fixes your struggles.



来源:https://stackoverflow.com/questions/20158362/where-do-i-find-the-instagram-media-id-of-a-image-having-access-token-and-follo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!