Where do I find the Instagram media ID of a image

后端 未结 15 1196

I\'m am looking for the MediaID of an Instagram image which has been uploaded. It should look like

1234567894561231236_33215652

相关标签:
15条回答
  • 2020-12-22 19:53

    You can actually derive the MediaId from the last segment of the link algorithmically using a method I wrote about here: http://carrot.is/coding/instagram-ids. It works by mapping the URL segment by character codes & converting the id into a base 64 number.

    For example, given the link you mentioned (http://instagram.com/p/Y7GF-5vftL), we get the last segment (Y7GF-5vftL) then we map it into character codes using the base64 url-safe alphabet (24:59:6:5:62:57:47:31:45:11_64). Next, we convert this base64 number into base10 (448979387270691659).

    If you append your userId after an _ you get the full id in the form you specified, but since the MediaId is unique without the userId you can actually omit the userId from most requests.

    Finally, I made a Node.js module called instagram-id-to-url-segment to automate this conversion:

    convert = require('instagram-id-to-url-segment');
    instagramIdToUrlSegment = convert.instagramIdToUrlSegment;
    urlSegmentToInstagramId = convert.urlSegmentToInstagramId;
    
    instagramIdToUrlSegment('448979387270691659'); // Y7GF-5vftL
    urlSegmentToInstagramId('Y7GF-5vftL'); // 448979387270691659
    
    0 讨论(0)
  • 2020-12-22 19:54

    So the most up-voted "Better Way" is a little deprecated so here is my edit and other solutions:

    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'];
    

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


    Shortcode Ajax Solution

    Certain Instagram urls use a shortened url syntax. This allows the client to just use the shortcode in place of the media id 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.

    Using Regexp:

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

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

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

    There is lots of other useful information, including the media id, in the returned RawData structure. Log it or look up the api documentation to see.


    Shortcode Conversion Solution

    You can actually convert your shortcode to the id fairly easily! Here's a simple way to do it in 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;
      }
    

    Note: If you want a more robust node.js solution, or want to see how you would convert it back, check out @Slang's module on npm.


    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 perform on the URL itself to get it, but I believe that requires too much effort so we will keep it simple. Either query the meta tag al:ios:url or iterate through the html. Since reading metatags is posted all over, I'll show you how to iterate.

    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.

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

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

    0 讨论(0)
  • 2020-12-22 19:55

    Your media ID is: 448979387270691659_45818965 This is how to get it.

    1. Go to instgram.com/username.
    2. Click the photo you want the id of.
    3. (Chrome instructions) right click on the photo (should be a popup image)
    4. Inspect element
    5. Search through the selected text, you should see something like this photo448979387270691659_45818965

    There should be your photo ID.

    For some reason, this only seems to work with the popup, and not the actual image URL.

    0 讨论(0)
  • 2020-12-22 19:56

    You can use the shortcode media API from instagram. If you use php you can use the following code to get the shortcode from the image's URL:

    $matches = [];
    
    preg_match('/instagram\.com\/p\/([^\/]*)/i', $url, $matches);
    
    if (count($matches) > 1) {
        $shortcode = $matches[1];
    }
    

    Then send a request to the API using your access token (Replace ACCESS-TOKEN with your token)

    $apiUrl = sprintf("https://api.instagram.com/v1/media/shortcode/%s?access_token=ACCESS-TOKEN", $shortcode);
    
    0 讨论(0)
  • 2020-12-22 19:59

    Here is python solution to do this without api call.

    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
    
    0 讨论(0)
  • 2020-12-22 19:59

    Same thing you can implement in 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')
    
    0 讨论(0)
提交回复
热议问题