45

I am pulling Facebook posts using facebook-graph API, now the problem is Image gets expired after few days.

I have the following URL for a single Image

Old Image URL which got expired

https://scontent-a.xx.fbcdn.net/hphotos-xfp1/v/l/t1.0-9/p180x540/14377_340369866155028_6836158858133154924_n.jpg?oh=7ed0d8818ad54fac851b036d24f5e674&oe=55579EE3

New Image working URL Is

https://scontent-sin1-1.xx.fbcdn.net/hphotos-xfa1/v/l/t1.0-9/14377_340369866155028_6836158858133154924_n.jpg?oh=2f7ad72fa36fc026ad2bdcc1b0284146&oe=55C87432

I am frustrated with this issue, what could be the solution of it?

Chiragkumar Thakar
  • 3,616
  • 5
  • 37
  • 49

3 Answers3

51

What i came to know from other community about this issue is

"You should not store Facebook CDN URLs for long time use – they can change over time.

Either request the actual image and copy that to your server – or request the current CDN URL regularly.

(You might be tempted to try other workarounds, like extracting the actual image source URL from the CDN link, but I would advise against that – because the format of that might change at any time as well.)"

Chiragkumar Thakar
  • 3,616
  • 5
  • 37
  • 49
  • 8
    Where can I find more information about Facebook CDN restrictions? I couldn't find anything about not keeping facebook cdn urls for long time. – Fran García Feb 26 '16 at 13:17
  • @FranGarcía it's not a restriction per se, it's pragmatism. Nothing stops you from keeping them long, but there's no practical use for that since they will expire sooner or later. – Michał Szajbe Jan 30 '18 at 10:03
  • sure, but I am wondering if there is any way of extracting the long term one. – Fran García Jan 30 '18 at 12:17
  • 1
    @FranGarcía this seems to be a very good solution https://stackoverflow.com/questions/34580136/facebook-app-login-how-to-control-expiration-date-of-an-image-url-i-got – Michał Szajbe Feb 05 '18 at 13:56
  • 8
    given the old url, how can I request the new one from the current CDN url? – miller the gorilla Apr 04 '18 at 11:26
15

you can not store facebook Images url for a long time, it expires for security purpose, so it would be a better solution to store images in your server.

  • 8
    For security purposes it's better to duplicate the image? – toxaq Jul 30 '17 at 04:16
  • @toxaq I imagine it's for Facebook's security rather than yours. ;) – Buttle Butkus Jan 24 '18 at 03:00
  • 4
    I suppose it is for security of Facebook income. You don't hotlink, they don't pay for server processing nor bandwidth to serve that avatar image again and again. – Zbyszek Feb 15 '18 at 13:59
  • 2
    But what's going to happen in any case where I need to display those images, I'm either going to request the image every time I need to serve it, in which case they are paying for processing and bandwidth every time, or I'm going to rerequest the image every few days, which could be more processing and bandwidth than just the hot links. – Chris Strickland Apr 26 '19 at 18:02
  • @toxaq its abcxyz for security purpose, so it would be a better solution to store images in your server duplicate the image. **Notice the comma** You should read "Let's eat, Grandma!" instead of "Let's eat Grandma!" – Pham X. Bach Aug 31 '23 at 02:59
6

Haven't updated for a long time and don't know if this works as before

You should store the original image URL for sure, and use a 302 redirect parser to get the CDN URL, one example is https://scontent-ort2-1.xx.fbcdn.net/v/t45.1600-4/120202220_23846099766190042_1642096590788171162_n.jpg?_nc_cat=108&ccb=2&_nc_sid=2aac32&_nc_ohc=CE0J2Ao5cYkAX_JJ0Me&_nc_ht=scontent-ort2-1.xx&oh=f48cbb1bec21e685e0cbaaf6782a61a1&oe=5FE056E5 and we can just guess oe=5FE056E5 means the expiration, as 5FE056E5(hexadecimal) -> 1608537829(decimal, in UTC), if you interpret this timestamp you will find the time is about a month later, and maybe we can guess the expiration is about a month after getting the CDN URL? To another similar case, you can refer to: https://stackoverflow.com/a/27596727/4721007

d0zingcat
  • 929
  • 8
  • 10
  • There is also another type of URL that does not have an `oe` parameter. Any Idea how to find expiry for those URLs? For example - https://external-lcy1-1.xx.fbcdn.net/safe_image.php?d=AQFqFwrYQZ-W0E3n&url=https%3A%2F%2Fbilder.bild.de%2Ffotos%2Fukraine-krieg-so-hart-treffen-swift-ausschluss-und-die-sanktionen-russland--und-uns-4dde2674191d4e3eb5656da5ba2859ee-79302638%2FBild%2F3%2Cclub%3Dbildplus.bild.jpg&ext=emg0&_nc_oe=6faa2&_nc_sid=64c8fc&ccb=3-5&_nc_hash=AQHaVrIBaC5eVXXm – Dinesh Mar 07 '22 at 00:10
  • If I change the `oe` parameter value I'll get a "URL signature mismatch" error. – Győző Papp Sep 29 '22 at 13:53