Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the "real" Facebook profile picture URL from graph API

Facebook graph API tells me I can get a profile picture of a user using

http://graph.facebook.com/517267866/picture?type=large

which works fine. However, when you type above URL into a browser, the actual address of the image is

http://profile.ak.fbcdn.net/profile-ak-snc1/v227/560/83/n517267866_1928.jpg

How can I get the second URL using the first one programmatically?

like image 440
ericbae Avatar asked Jul 27 '10 08:07

ericbae


People also ask

How do I copy image URL from Facebook app?

2. Right-click the image and click the option to copy its URL. Depending on the browser you use, the option reads "Copy Image Location," "Copy Image Address," "Copy Link" or "Copy Image URL." In Internet Explorer, click the "Properties" option and copy the URL from the Properties window.

How do I post a photo from Facebook Graph API?

Publishing an Photo With that granted, you can upload a photo by issuing an HTTP POST request with the photo content and an optional description to one these to Graph API connections: https://graph.facebook.com/USER_ID/photos - The photo will be published to an album created for your app.


2 Answers

The first URL gives a HTTP 302 (temporary redirect) to the second. So, to find the second URL programatically, you could issue a HTTP request for the first URL and get the Location header of the response.

That said, don't rely on the second URL being pemanent. Reading a little in to the HTTP response code (of 302 as opposed to a permanent 301), it is possible Facebook changes those URLs on a regular basis to prevent people from—for example—using their servers to host images.


Edit: Notice that the CDN URL the OP posted is now a 404, so we know that we cannot rely on the URL being long-lived. Also, if you're linking to the Graph API from an <img> on a SSL-secured page, there's a parameter you have to add make sure you use https://graph.facebook.com.


Update: The API has added a parameter – redirect=false – which causes JSON to be returned rather than a redirect. The retruned JSON includes the CDN URL:

{    "data": {       "url": "http://profile.ak.fbcdn.net/...",       "is_silhouette": false    } } 

Again, I wouldn't rely on this CDN URL being long-lived. The JSON response is sent with permissive CORS headers, so you're free to do this client-side with XHR requests.

like image 85
josh3736 Avatar answered Oct 16 '22 02:10

josh3736


http://graph.facebook.com/517267866/?fields=picture&type=large

Would return the URL in JSON

like image 21
Adfsd Avatar answered Oct 16 '22 03:10

Adfsd