Why downloading Facebook images with requests.get() gives corrupted files?

310 Views Asked by At

I am a very new to Python and Facebook Graph API and hope you can help me out with this:

I have writted (in Python) a peace of code that uploads images tu a page on facebook (in a post, so it contains some text too) and this works as expected. Now I am trying to write a peace of code capable of downloading thhe image inside a post (given post_id). Unfortunately I always get "file corrupted " error.

Here is the code I use to download the image:

# this function uploads an image from a web url
ret = upload_img_to_fb(url) 

# decode JSON from the request
post_id = json.loads(ret)

ret = get_json_from_fb_postID(post_id['post_id'])
perm_url = json.loads(ret)
print('Perm url = ' + perm_url['permalink_url'] + '\n')

img_response = requests.get(perm_url['permalink_url'])
image = open("foto4.jpg","wb")
image.write(img_response.content)
image.close()

Now, the print will print the following:

Perm url = https://www.facebook.com/102956292308044/photos/a.103716555565351/107173241886349/?type=3

which, acording to what I understood makes everything wrong because it is not a picture, even if a picture is displayed on the screen. So I right clicked the pic and opened it's link and I got:

https://scontent.fbzo1-2.fna.fbcdn.net/v/t39.30808-6/273008252_107171558553184_3697853178128736286_n.jpg?_nc_cat=103&ccb=1-5&_nc_sid=9267fe&_nc_ohc=d0ZvWSTzIogAX-PsuzN&_nc_ht=scontent.fbzo1-2.fna&oh=00_AT8GWh0wDHgB6tGCzHuPE2VZFus9EgWhllaJfVkZ-Nqtow&oe=620465E4

and if I pass this last link as parameter to img_response = requests.get() it works.

How do I get around this?

0

There are 0 best solutions below