我在哪里可以找到图片的Instagram媒体ID(拥有访问令牌并关注图片所有者)


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

我有instagram照片的图片页面url。现在我想把这个图片页面的url发送到instagramapi,并获得媒体id和所有者用户名。如果映像是公共的,下面的api调用可以很好地工作,但如果映像是私有的,它将失败,并给我无媒体匹配

我已经在关注图像所有者,并拥有用户instagramapi的访问令牌。那么,如何使用php或javascript获取具有图像页面url的媒体id和图像所有者名称呢?我还需要使用其他处理私人图像的api吗?

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

所以有很多折旧的解决方案,下面是我迄今为止正在运行的编辑/解决方案。

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);
        }
   }
});

所以这只是@George代码的更新版本,目前正在运行。然而,我提出了其他可以避免ajax请求的解决方案:

短码密钥解决方案

某些Instagram url使用缩短的url语法。这允许客户端只使用短代码来代替正确请求的媒体创意IF。

一个示例短代码url如下所示:https://www.instagram.com/p/Y7GF-5vftL/

"Y7GF-5vftL"是图片的快捷代码。

                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);
                }

在相同的作用域中,"Key"将包含您的快捷代码。现在要请求,比如说,一张低分辨率的图片,使用这个短代码,你会做如下操作:

//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;
                        }
                    });
                }

在返回的RawData结构中还有许多其他有用的信息。记录它或查找api文档以查看。

整页解决方案那么,如果你有一个完整的instagram页面的URL,比如:https://www.instagram.com/p/BAYYJBwi0Tssh605CJP2bmSuRpm_Jt7V_S8q9A0/

实际上,你可以阅读HTML来找到一个包含媒体ID的元属性。还有一些其他算法可以在URL本身上预成型来获得它,但我认为这需要付出太多的努力,所以我们会保持简单。

注意:这有点不稳定,很容易被修补。此方法不适用于使用预览框的页面。因此,如果你在点击某人个人资料中的图片时给它当前的HTML,这将破坏并返回错误的媒体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);
}

现在,有很多不同的方法可以使用Instagram的api来获取媒体ID。希望有一种能解决你的困难。