我在哪里可以找到图像的Instagram媒体ID(具有访问令牌并跟随图像所有者)? [英] Where do I find the Instagram media ID of a image (having access token and following the image owner)?

查看:65
本文介绍了我在哪里可以找到图像的Instagram媒体ID(具有访问令牌并跟随图像所有者)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有instagram照片的图像页面网址.现在,我想将此图像页面的网址发送到instagram api,并获取媒体ID和所有者用户名.如果图片是公开的,则以下api调用效果很好,但是如果图片是私有的,它将失败并给我没有媒体匹配.

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.

我已经关注了图片所有者,并具有访问用户instagram api的令牌.那么,如何使用php或javascript获取具有图像页面URL的媒体ID和图像所有者名称?我还需要用户处理私有图像的其他任何API吗?

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/

推荐答案

所以这里有很多折旧的解决方案,所以这是我截至目前的修改/解决方案.

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

因此,这只是@George代码的更新版本,目前正在运行.但是,我提出了其他解决方案来避免ajax请求:

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:

短代码密钥解决方案

某些Instagram URL使用较短的URL语法.这样,客户就可以使用短代码代替正确请求的媒体提示.

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.

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

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

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

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

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

整页解决方案那么,如果您拥有完整的instagram页面的网址,例如: https://www.instagram.com/p/BAYYJBwi0Tssh605CJP2bmSuRpm_Jt7V_S8q9A0/

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

实际上,您可以阅读HTML来查找包含媒体ID的元属性.您还可以对URL本身执行其他几种算法来获取它,但是我认为这需要花费大量精力,因此我们将使其保持简单.

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.

注意:这有点不稳定,很容易被修补.此方法不适用于使用预览框的页面.因此,如果您在某人的个人资料中单击图片时为其提供当前的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的多种方法.希望能解决您的难题.

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

这篇关于我在哪里可以找到图像的Instagram媒体ID(具有访问令牌并跟随图像所有者)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆