Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instagram web api get: :ERR_BLOCKED_BY_RESPONSE

Tags:

instagram

I created a script that extracts photos in the gallery of a certain profile… Using instagram-web-api

Unfortunately now it no longer works, instagram does not return the image of the media This is the mistake: ERR_BLOCKED_BY_RESPONSE

Instagram has changed it’s CORS policy recently? How I can fix?

like image 647
Vypertek Avatar asked Apr 27 '21 08:04

Vypertek


4 Answers

now 100% working. You can try this.

corsDown

  • Using the Google translation vulnerability, it can display any image URL, with or without permission. All these processes are done by the visitor's IP and computer.
like image 196
cryptosu Avatar answered Nov 14 '22 16:11

cryptosu


The same issue was reported recently on https://github.com/restyler/instagram-php-scraper.

Instagram now sets cross-origin-resource-policy: same-origin if it doesn't like the "referer" which your browser sends to cdninstagram domain when loading image.

One of ways to mitigate this might be to create a simple image proxy which will work on your domain, download the image from instagram server to your server, and then output it to the browser.

Here is an example of CloudFlare image proxy:

https://gist.github.com/restyler/6c51e3ad20d7596e799d76e87cf93236

Not that efficient, but can be self-hosted, PHP implementation:

https://github.com/restyler/inwidget-proxified/blob/master/imgproxy.php

It might be a good idea to add additional caching layer on the proxy to reduce the amount of duplicate image requests to instagram cdn servers.

When you have your image proxy running, you just need to replace all instagram image srcs to the proxified versions.

like image 21
superjet Avatar answered Oct 09 '22 16:10

superjet


for php; I changed my img src to this and it works like charm! Assume that $image is the instagram image cdn link came from instagram page:

'data:image/jpg;base64,'.base64_encode(file_get_contents($image))

EDIT FOR BETTER SOLUTION

I have also noticed that, this method is causing so much latency. So I have changed my approach and now using a proxy php file (also mentioned on somewhere on stackoverflow but I don't remember where it is)

This is my common proxy file content:

<?php
function ends_with( $haystack, $needle ) {
    return substr($haystack, -strlen($needle))===$needle;
}

if (!in_array(ini_get('allow_url_fopen'), [1, 'on', 'true'])) {
   die('PHP configuration change is required for image proxy: allow_url_fopen setting must be enabled!');
}

$url = isset($_GET['url']) ? $_GET['url'] : null;


if (!$url || substr($url, 0, 4) != 'http') {
    die('Please, provide correct URL');
}

$parsed = parse_url($url);

if ((!ends_with($parsed['host'], 'cdninstagram.com') && !ends_with($parsed['host'], 'fbcdn.net')) || !ends_with($parsed['path'], 'jpg')) {
    die('Please, provide correct URL');
}

// instagram only has jpeg images for now..
header("Content-type: image/jpeg");
readfile( $url );

?>

Then I have just converted all my instagram image links to this (also don't forget to use urlencode function on image links):

./proxyFile.php?url=https://www.....

It worked like charm and there is no latency anymore.

like image 9
littlealien Avatar answered Nov 14 '22 16:11

littlealien


I have the same problem, when I try to load a Instagram's pictures url (I tried with 3 IP addresses), I see this on the console:

Failed to load resource: net::ERR_BLOCKED_BY_RESPONSE

You can see it here, the Instagram image doesn't load (Actually, when I paste this url on google it works, but Instagram puts a timestamp on there pictures so, it's possible it won't work for you).

It's very recent, 3 days ago, it works with no issues.

<img src="https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s320x320/176283370_363930668352575_6367243109377325650_n.jpg?tp=1&_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_ohc=nC7FG1NNChYAX8wSL7_&edm=ABfd0MgBAAAA&ccb=7-4&oh=696d56547f87894c64f26613c9e44369&oe=60AF5A34&_nc_sid=7bff83">
like image 3
Lior Avatar answered Nov 14 '22 17:11

Lior