42

我正在寻找MediaID已上传的 Instagram 图片。它应该看起来像

1234567894561231236_33215652

我发现最后一组整数是usersID

例如:这是直接图片的链接,但是我看不到mediaID正确的格式?

http://distilleryimage11.ak.instagram.com/d33aafc8b55d11e2a66b22000a9f09de_7.jpg

虽然这是链接

http://instagram.com/p/Y7GF-5vftL/

我不希望使用 API,因为我只需要来自所选图像的 MediaID。

4

16 回答 16

101

这是一个更好的方法:

http://api.instagram.com/oembed?url=http://instagram.com/p/Y7GF-5vftL/

渲染为 json 对象,您可以轻松地从中提取媒体 ID ---

例如,在 PHP

$api = file_get_contents("http://api.instagram.com/oembed?url=http://instagram.com/p/Y7‌​GF-5vftL/");      
$apiObj = json_decode($api,true);      
$media_id = $apiObj['media_id'];

例如,在 JS

$.ajax({     
    type: 'GET',     
    url: 'http://api.instagram.com/oembed?callback=&url=http://instagram.com/p/Y7GF-5vftL‌​/',     
    cache: false,     
    dataType: 'jsonp',     
    success: function(data) {           
        try{              
            var media_id = data[0].media_id;          
        }catch(err){}   
    } 
});
于 2013-09-02T07:46:37.880 回答
21

因此,投票最多的“更好的方式”有点被弃用,所以这是我的编辑和其他解决方案:

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) {
       var MediaID = data.media_id;
   }
});

PHP

$your_url = "" //Input your url
$api = file_get_contents("http://api.instagram.com/oembed?callback=&url=" . your_url);      
$media_id = json_decode($api,true)['media_id'];

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


简码 Ajax 解决方案

某些 Instagram 网址使用缩短的网址语法。如果请求正确,这允许客户端仅使用短代码代替媒体 ID。

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

Y7GF-5vftL是您的图片简码。

使用正则表达式:

var url = "https://www.instagram.com/p/Y7GF-5vftL/"; //Define this yourself
var Key = /p\/(.*?)\/$/.exec(url)[1];

在同一范围内,Key将包含您的简码。现在要使用此短代码请求低分辨率图片,您可以执行以下操作:

    $.ajax({
        type: "GET",
        dataType: "json",
        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 结构中还有许多其他有用的信息,包括媒体 ID。记录它或查找 api 文档以查看。


简码转换解决方案

实际上,您可以相当轻松地将您的简码转换为 id!这是在javascript中执行此操作的简单方法:

function shortcodeToInstaID(Shortcode) {
      var char;
      var id = 0;
      var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
      for (var i = 0; i < Shortcode.length; i++) {
          char = Shortcode[i];
          id = (id * 64) + alphabet.indexOf(char);
      }
      return id;
  }

注意:如果您想要一个更强大的 node.js 解决方案,或者想了解如何将其转换回来,请查看npm 上的 @Slang 模块


整页解决方案

那么,如果您有完整 Instagram 页面的 URL,例如:https ://www.instagram.com/p/BAYYJBwi0Tssh605CJP2bmSuRpm_Jt7V_S8q9A0/

好吧,您实际上可以阅读 HTML 以找到包含媒体 ID 的元属性。您还可以对 URL 本身执行一些其他算法来获取它,但我相信这需要太多努力,所以我们会保持简单。查询元标记 al:ios:url或遍历 html。由于阅读元标记已经发布,我将向您展示如何迭代。

注意:这有点不稳定,很容易被修补。此方法不适用于使用预览框的页面。因此,如果您在单击某人个人资料中的图片时为其提供当前 HTML,这将中断并返回错误的媒体 ID。

function getMediaId(HTML_String) {
  var MediaID = "";
  var e = HTML_String.indexOf("al:ios:url") + 42; //HTML_String is a variable that contains all of the HTML text as a string for the current document. There are many different ways to retrieve this so look one up. 
  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);
  }
  return MediaID;
}

你去了,一堆不同的方法来使用 Instagram 的 api 来获取媒体 ID。希望一个解决你的斗争。

于 2016-01-11T15:31:41.457 回答
17

这是一个更好的方法:

没有 API 调用!我将 a 转换media_id为 ashortcode作为额外奖励。

基于俚语在计算转换方面的 惊人工作Nathan 在 php 中将 base10 转换为 base64的工作。并且rgbflawed 的 工作将其转换回另一种方式(使用修改后的字母表)。#团队努力

function mediaid_to_shortcode($mediaid){

    if(strpos($mediaid, '_') !== false){
        $pieces = explode('_', $mediaid);
        $mediaid = $pieces[0];
        $userid = $pieces[1];
    }

    $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
    $shortcode = '';
    while($mediaid > 0){
        $remainder = $mediaid % 64;
        $mediaid = ($mediaid-$remainder) / 64;
        $shortcode = $alphabet{$remainder} . $shortcode;
    };

    return $shortcode;

}

function shortcode_to_mediaid($shortcode){

    $alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';

    $mediaid = 0;

    foreach(str_split($shortcode) as $letter) {
        $mediaid = ($mediaid*64) + strpos($alphabet, $letter);
    }

    return $mediaid;

}
于 2016-05-16T03:19:11.830 回答
17

这是一个更好的方法(没有 Instagram API):

  1. 获取您需要媒体 ID 的 Instagram 图片的永久链接 URL。
    http://instagram.com/p/Y7GF-5vftL/

  2. ?__a=1在永久链接末尾添加
    http://instagram.com/p/Y7GF-5vftL/?__a=1

在浏览器中运行该链接, Instagram 将以 JSON 格式提供图像的所有元数据,您可以从中轻松恢复图像 ID 和其他有价值的数据。

JSON 响应将如下所示:

{
  "graphql": {
    "shortcode_media": {
      "__typename": "GraphImage",
      "id": "448979387270691659",
      "shortcode": "Y7GF-5vftL",
      "dimensions": {
        "height": 612,
        "width": 612
      },
      "gating_info": null,
      "fact_check_overall_rating": null,
      "fact_check_information": null,
      "sensitivity_friction_info": null,
      "sharing_friction_info": {
        "should_have_sharing_friction": false,
        "bloks_app_url": null
      },
      "media_overlay_info": null,
      "media_preview": null,
      "display_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
      "display_resources": [
        {
          "src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
          "config_width": 640,
          "config_height": 640
        },
        {
          "src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
          "config_width": 750,
          "config_height": 750
        },
        {
          "src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
          "config_width": 1080,
          "config_height": 1080
        }
      ],
      "accessibility_caption": "Photo by Steven Caelius Thirlwall on May 05, 2013.",
      "is_video": false,
      "tracking_token": "eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjpmYWxzZSwidXVpZCI6IjFjOTliZDQ5MTU0YzQ2ODY4OGY5MmM4ODgwYWQ4NGQzNDQ4OTc5Mzg3MjcwNjkxNjU5Iiwic2VydmVyX3Rva2VuIjoiMTYzNzY4NDc1NjI4MHw0NDg5NzkzODcyNzA2OTE2NTl8MzExMTA2NDAyfDE2ZGMyNjk3M2M1YWY5YWEzOTNhZTY5YzEzYzU4YjM5NWI0YWQ3MjY1OGQxOTg4YWQ2OWUxYmI3ZjkyNzU5ZDQifSwic2lnbmF0dXJlIjoiIn0=",
      "upcoming_event": null,
      "edge_media_to_tagged_user": {
        "edges": []
      },
      "edge_media_to_caption": {
        "edges": [
          {
            "node": {
              "text": "New ankle biter! #Beagle #pup #shoes #blueisthecolor #cute #iwantone"
            }
          }
        ]
      },
      "can_see_insights_as_brand": false,
      "caption_is_edited": false,
      "has_ranked_comments": false,
      "like_and_view_counts_disabled": false,
      "edge_media_to_parent_comment": {
        "count": 902,
        "page_info": {
          "has_next_page": true,
          "end_cursor": "QVFBZFlTbmNsVWh1T2dIS1I3Mm8yRl9DdGVQMHV6VXg5cjQxZzlYa0gxQ1NFZGc5a1FfWHhIMTlURU84dlBCcG5QX1I2VXhvVXNpbGRGWlktNG5FTjdRTQ=="
        },
        "edges": [
          {
            "node": {
              "id": "17916614443887715",
              "text": "@h___ep10",
              "created_at": 1632633242,
              "did_report_as_spam": false,
              "owner": {
                "id": "7472159900",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/33736445_386998071786552_2814599361646821376_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=100&_nc_ohc=kaD1UHiVnU8AX9oWv0I&edm=AABBvjUBAAAA&ccb=7-4&oh=cabcfaac8d48b63b41f0e9a32bb0282f&oe=61A38CDE&_nc_sid=83d603",
                "username": "x._raha.moradi_.x"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17894221358363984",
              "text": "@zahraroshanikia",
              "created_at": 1632633244,
              "did_report_as_spam": false,
              "owner": {
                "id": "46440556914",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/159256912_431734447917250_5641996282890612011_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=110&_nc_ohc=y2JgjQ2mc6EAX8U9OnZ&edm=AABBvjUBAAAA&ccb=7-4&oh=95911a34aea9f1fe0f6ae941154de86b&oe=61A4F7B5&_nc_sid=83d603",
                "username": "saman.wx68"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17931125308699029",
              "text": "@azarimani55",
              "created_at": 1632633244,
              "did_report_as_spam": false,
              "owner": {
                "id": "33210479560",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/94635563_230560861379228_5419754827787796480_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=108&_nc_ohc=7JyeFrDCzcMAX_uDrhk&edm=AABBvjUBAAAA&ccb=7-4&oh=5599a78e5508ca3827b5c580c4e5daf0&oe=61A3CB8E&_nc_sid=83d603",
                "username": "alirezaekhteraee"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "18164016367163647",
              "text": "@mbahrambagi436",
              "created_at": 1632633245,
              "did_report_as_spam": false,
              "owner": {
                "id": "45915987079",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/148716764_428606351708713_8819828798031017255_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=101&_nc_ohc=ZKBGqc8f6xAAX_roxPf&edm=AABBvjUBAAAA&ccb=7-4&oh=0b8ed79236bf5e97dde3a4621a1cc647&oe=61A3B2BD&_nc_sid=83d603",
                "username": "xx77llxxrdd"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17921916424799580",
              "text": "@soheila.rezvan",
              "created_at": 1632633246,
              "did_report_as_spam": false,
              "owner": {
                "id": "47289012029",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "ha_midreza2843"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17907564257125873",
              "text": "@paria_niazi80",
              "created_at": 1632633247,
              "did_report_as_spam": false,
              "owner": {
                "id": "46492412778",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/260070060_924734438469169_5591668570031114480_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=101&_nc_ohc=fl-bN9uNz9sAX_v_XtR&edm=AABBvjUBAAAA&ccb=7-4&oh=a875da2937ac853d3111499bc32fa452&oe=61A43C46&_nc_sid=83d603",
                "username": "ricky_gutierrezz4"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17922561976794017",
              "text": "@memmm.mom",
              "created_at": 1632633247,
              "did_report_as_spam": false,
              "owner": {
                "id": "47086299475",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "izadiizadi2"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "18130172461216294",
              "text": "@221bcc",
              "created_at": 1632633247,
              "did_report_as_spam": false,
              "owner": {
                "id": "47371753534",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "karimov_94422"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17932389193640009",
              "text": "@tehran___la",
              "created_at": 1632633248,
              "did_report_as_spam": false,
              "owner": {
                "id": "46622156073",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "rynwrynw627"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17935588228640366",
              "text": "@sepideh.m.i.r",
              "created_at": 1632633248,
              "did_report_as_spam": false,
              "owner": {
                "id": "5905800882",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/160110991_451499282792610_787693123774809640_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=106&_nc_ohc=CBgOU0M1MPYAX8m7vvT&edm=AABBvjUBAAAA&ccb=7-4&oh=18a215ed2c3a0cd0e58a905e22cd0c8f&oe=61A489B4&_nc_sid=83d603",
                "username": "__mohammad__677"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17854564814634983",
              "text": "@nilooofar5828",
              "created_at": 1632633249,
              "did_report_as_spam": false,
              "owner": {
                "id": "46091925856",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "hsheh6154"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17907793715125698",
              "text": "@oran31201816",
              "created_at": 1632633249,
              "did_report_as_spam": false,
              "owner": {
                "id": "44672434922",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/149881733_183016319827876_8572211010018355650_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=104&_nc_ohc=JgjvztBQGrIAX8nLUiM&tn=hRpIRP1GgMkkZ4n7&edm=AABBvjUBAAAA&ccb=7-4&oh=d9e408cf78f195a83f8bb7beec8c22c1&oe=61A360EC&_nc_sid=83d603",
                "username": "barcelona10_trol"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          }
        ]
      },
      "edge_media_to_hoisted_comment": {
        "edges": []
      },
      "edge_media_preview_comment": {
        "count": 902,
        "edges": []
      },
      "comments_disabled": false,
      "commenting_disabled_for_viewer": false,
      "taken_at_timestamp": 1367742535,
      "edge_media_preview_like": {
        "count": 13823,
        "edges": [
          {
            "node": {
              "id": "750983393",
              "is_verified": false,
              "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/245113529_402184758053910_6991076024439577169_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=106&_nc_ohc=TPs3jHagdx4AX_JdrO5&tn=hRpIRP1GgMkkZ4n7&edm=AABBvjUBAAAA&ccb=7-4&oh=95f6532e2d737a91e3848fa0525319e4&oe=61A4C0D1&_nc_sid=83d603",
              "username": "josepkoray"
            }
          }
        ]
      },
      "edge_media_to_sponsor_user": {
        "edges": []
      },
      "is_affiliate": false,
      "is_paid_partnership": false,
      "location": null,
      "viewer_has_liked": false,
      "viewer_has_saved": false,
      "viewer_has_saved_to_collection": false,
      "viewer_in_photo_of_you": false,
      "viewer_can_reshare": true,
      "owner": {
        "id": "45818965",
        "is_verified": false,
        "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/38792937_2121773964703196_2247098649857228800_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=108&_nc_ohc=LyE1N2PfQ3IAX9R25jq&edm=AABBvjUBAAAA&ccb=7-4&oh=29912db673500a2adc9df6ec7241d8c7&oe=61A471D2&_nc_sid=83d603",
        "username": "taz4535",
        "blocked_by_viewer": false,
        "restricted_by_viewer": false,
        "followed_by_viewer": false,
        "full_name": "Steven Caelius Thirlwall",
        "has_blocked_viewer": false,
        "is_embeds_disabled": false,
        "is_private": false,
        "is_unpublished": false,
        "requested_by_viewer": false,
        "pass_tiering_recommendation": false,
        "edge_owner_to_timeline_media": {
          "count": 236
        },
        "edge_followed_by": {
          "count": 700
        }
      },
      "is_ad": false,
      "edge_web_media_to_related_media": {
        "edges": []
      },
      "coauthor_producers": [],
      "edge_related_profiles": {
        "edges": []
      }
    }
  }
}

例如,?__a=1即使在 Instagram用户永久链接上,您也可以使用相同的结尾,https://www.instagram.com/taz4535/?__a=1 并且您将在回复 JSON中获得非常有价值的用户数据。

最好的是,这一切都是在没有 Instagram API 身份验证的情况下完成的!

于 2019-10-22T17:39:48.413 回答
13

实际上,您可以使用我在这里写的方法从算法的最后一段中派生 MediaId:http: //carrot.is/coding/instagram-ids。它的工作原理是通过字符代码映射 URL 段并将 id 转换为 base 64 数字。

例如,给定您提到的链接 ( http://instagram.com/p/Y7GF-5vftL ),我们得到最后一段 ( ),然后我们使用base64 url​​-safe 字母表( )Y7GF-5vftL将其映射为字符代码。接下来,我们将这个 base64 数字转换为 base10 ( )。24:59:6:5:62:57:47:31:45:11_64448979387270691659

如果您在获取完整 id 后附加您的 userId,_则您指定的表单中,但由于 MediaId 在没有 userId 的情况下是唯一的,您实际上可以从大多数请求中省略 userId。

最后,我制作了一个名为instagram-id-to-url-segment的 Node.js 模块来自动进行这种转换:

convert = require('instagram-id-to-url-segment');
instagramIdToUrlSegment = convert.instagramIdToUrlSegment;
urlSegmentToInstagramId = convert.urlSegmentToInstagramId;

instagramIdToUrlSegment('448979387270691659'); // Y7GF-5vftL
urlSegmentToInstagramId('Y7GF-5vftL'); // 448979387270691659
于 2015-07-30T02:18:16.070 回答
8

试试这个问题的解决方案: 如何从 twitter 实体获取直接的 Instagram 链接?

您可以通过将 /media/ 附加到 URL 来仅获取图像。使用您的

您甚至可以指定尺寸,

t(缩略图)、m(中)、l(大)之一。默认为 m。

所以对于缩略图: http: //instagr.am/p/QC8hWKL_4K/media/ ?size=t

于 2014-01-20T03:27:01.263 回答
6

您的媒体 ID 是:448979387270691659_45818965 这是获取它的方法。

  1. 转到 instgram.com/username。
  2. 单击您想要其 ID 的照片。
  3. (Chrome使用说明)右击照片(应该是弹出图片)
  4. 检查元素
  5. 搜索选定的文本,您应该会看到类似这样的内容photo448979387270691659_45818965

应该有您的带照片的身份证件。

出于某种原因,这似乎只适用于弹出窗口,而不适用于实际的图像 URL。

于 2013-05-30T23:19:47.813 回答
6

这是无需 api 调用即可执行此操作的 python 解决方案。

def media_id_to_code(media_id):
    alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
    short_code = ''
    while media_id > 0:
        remainder = media_id % 64
        media_id = (media_id-remainder)/64
        short_code = alphabet[remainder] + short_code
    return short_code


def code_to_media_id(short_code):
    alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
    media_id = 0;
    for letter in short_code:
        media_id = (media_id*64) + alphabet.index(letter)

    return media_id
于 2017-05-09T18:21:52.760 回答
3

在纯 JS 中(前提是您的浏览器可以处理 XHR,每个主流浏览器 [包括 IE > 6] 都可以):

function igurlretrieve(url) {

  var urldsrc = "http://api.instagram.com/oembed?url=" + url;

  //fetch data from URL data source
  var x = new XMLHttpRequest();
  x.open('GET', urldsrc, true);
  x.send();

  //load resulting JSON data as JS object
  var urldata = JSON.parse(x.responseText);

  //reconstruct data as "instagram://" URL that can be opened in iOS app
  var reconsturl = "instagram://media?id=" + urldata.media_id;
  return reconsturl;

}

如果这是您的目标——只需在 Instagram iOS 应用程序中打开页面,这正是它的目的——应该这样做,特别是如果您不想承担许可费。

于 2013-12-17T06:35:05.687 回答
1

编辑

iOS Instagram 应用程序现在已注册常规 http 链接以在 Instagram 应用程序中打开,并且不再需要这种深度链接方法。

老的

Swift 4 短代码解析解决方案

private static func instagramDeepLinkFromHTTPLink(_ link: String) -> String? {
    guard let shortcode = link.components(separatedBy: "/").last else { return nil }

    // algorithm from https://stackoverflow.com/a/37246231/337934
    let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
    var mediaId: Int = 0
    for (_, char) in shortcode.enumerated() {
        guard let index = alphabet.index(of: char) else { continue }
        mediaId = (mediaId * 64) + index.encodedOffset
    }

    return "instagram://media?id=\(mediaId)"
}
于 2018-06-10T22:15:36.420 回答
1

你可以在 Python 中实现同样的事情——

import requests,json

def get_media_id(media_url):
   url = 'https://api.instagram.com/oembed/?callback=&url=' + media_url
   response = requests.get(url).json()
   print(response['media_id'])

get_media_id('MEDIA_URL')
于 2018-09-16T19:25:21.613 回答
0

您可以使用 instagram 的短代码媒体 API。如果您使用 php,则可以使用以下代码从图像的 URL 获取短代码:

$matches = [];

preg_match('/instagram\.com\/p\/([^\/]*)/i', $url, $matches);

if (count($matches) > 1) {
    $shortcode = $matches[1];
}

然后使用您的访问令牌向 API 发送请求(替换ACCESS-TOKEN为您的令牌)

$apiUrl = sprintf("https://api.instagram.com/v1/media/shortcode/%s?access_token=ACCESS-TOKEN", $shortcode);
于 2017-08-21T14:41:09.883 回答
0

Instagram在 2019 年末弃用了其旧版 API 以支持Basic Display API

Basic Display API中,您应该使用以下 API 端点来获取媒体 id。您需要提供有效的访问令牌

https://graph.instagram.com/me/media?fields=id,caption&access_token={access-token}

您可以在此处阅读有关如何在Facebook 开发人员门户上配置测试帐户和生成访问令牌的信息。

是另一篇文章,它还描述了如何获取访问令牌。

于 2020-09-23T08:36:27.760 回答
0
  1. Instagram 媒体 ID 到简码

  2. Instagram 短代码到媒体 ID

var bigint = require( 'big-integer' )

var lower = 'abcdefghijklmnopqrstuvwxyz';
var upper = lower.toUpperCase();
var numbers = '0123456789'
var ig_alphabet =  upper + lower + numbers + '-_'
var bigint_alphabet = numbers + lower

function toShortcode( longid )
{
var o = bigint( longid ).toString( 64 )
return o.replace(/<(\d+)>|(\w)/g, (m,m1,m2) =>
{
    return ig_alphabet.charAt( ( m1 )
      ? parseInt( m1 ) 
      : bigint_alphabet.indexOf( m2 ) )
    });
}

function fromShortcode( shortcode )
{
    var o = shortcode.replace( /\S/g, m =>
    {
    var c = ig_alphabet.indexOf( m )
    var b = bigint_alphabet.charAt( c ) 
    return ( b != "" ) ? b : `<${c}>`
    } ) 
    return bigint( o, 64 ).toString( 10 )
}
toShortcode(  '908540701891980503' ) // s.b. 'ybyPRoQWzX'
fromShortcode( 'ybyPRoQWzX' ) // s.b. '908540701891980503'
于 2021-07-23T09:31:52.570 回答
-1

右键单击照片并在新选项卡/窗口中打开。右键单击inspect element。搜索:

instagram://media?id=

这会给你:

instagram://media?id=############# /// ID

完整的 id 构造来自

photoID_userID

要获取用户 ID,请搜索:

instapp:owner_user_id将在 content=

于 2016-11-24T10:29:40.773 回答
-1

有一段时间我不得不经常自己提取媒体 ID,所以我编写了自己的脚本(很可能是基于这里的一些示例)。连同我经常使用的其他小脚本,我开始将它们上传到 www.findinstaid.com 以便我自己快速访问。

我添加了输入用户名以获取 12 个最新帖子的媒体 ID 的选项,或输入 URL 以获取特定帖子的媒体 ID 的选项。

如果方便,每个人都可以使用该链接(我在网站上没有任何添加或任何其他金钱利益 - 我只有在“审核”选项卡上的一个推荐链接,指向我也拥有的 www.auditninja.io,而且在这个网站上,没有附加或金钱利益 - 只是爱好项目)。

于 2019-07-02T17:56:16.883 回答