Getting the "real" Facebook profile picture URL from graph API
Asked Answered
I

12

74

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?

Illusionist answered 27/7, 2010 at 8:1 Comment(5)
Why do you need the "real" oneShaylynn
that way I can save the URL and call it directly without making the API request?Illusionist
you can still call it directly. it will still workLantz
@Lantz , but both give different results when user changes his picture.Protozoon
I realise it's 10 years later (how amazing is stackoverflow ? ) but that URL returns the cover photo not the user's profile picture.Cruiser
S
72

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 parameterredirect=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.

Scipio answered 27/7, 2010 at 8:10 Comment(3)
makes sense. this is what I expected. thanks for the response!Illusionist
Does it makes sense to send down cdn urls from the server if you are doing a game leader board and only want to expose a users profile picture not their user ID? I'm not saying count on the CDN but just make the call server side to hide fbID details from client.Cavil
How to avoid the image file download? It seems that the image url, while returning 200 is downloaded by the browser instead of being displayed. Maybe because the response headers are content-disposition: attachment and content-type: image/jpeg.Palmieri
S
36

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

Would return the URL in JSON

Shumate answered 10/4, 2011 at 14:38 Comment(5)
Returns the small picture URL.Derosa
Ditto - this returns the small imageSeismism
if you really want to fetch it this way, use graph.facebook.com/517267866/?fields=picture.type(large)Delvalle
@Delvalle You should write this up as a separate answer: the specific syntax 'fields=picture.type(large)' was the only solution for me to get an URL to a large profile image as a JSON response from Facebook.Rutkowski
<page_or_user_id>/picture?type=large or <page_or_user_id>?fields=picture.type(large) both work for me... but the image returned is completely different from the profile image I see if I actually visit the page.Lumper
B
16

I realize this is late, but there is another way to obtain the URL of the profile image.

To your original url, you can add the parameter redirect=false to obtain the actual URL of the image you'd normally be redirected to.

So, the new request would look like http://graph.facebook.com/517267866/picture?type=large&redirect=false. This will return a JSON object containing the URL of the picture and a boolean is_silhouette (true if the picture is the default Facebook picture).

The picture will be of the size you specified, as well. You can test this additionally by adding dimensions: http://graph.facebook.com/517267866/picture?type=large&redirect=false&width=400&height=400

Biebel answered 4/10, 2012 at 14:29 Comment(0)
G
7

For anyone else looking to get the profile pic in iOS:

I just did this to get the user's Facebook pic:

NSString *profilePicURL = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large", fbUserID];

where 'fbUserID' is the Facebook user's profile ID.

This way I can always just call the url in profilePicURL to get the image, and I always get it, no problem. If you've already got the user ID, you don't need any API requests, just stick the ID into the url after facebook.com/.

FYI to anyone looking who needs the fbUserID in iOS:

if (FBSession.activeSession.isOpen) {
    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection,
       NSDictionary<FBGraphUser> *user,
       NSError *error) {
         if (!error) {
             self.userName = user.name;
             self.fbUserID = user.id;
         }
     }];
}

You'll need an active FBSession for that to work (see Facebook's docs, and the "Scrumptious" example).

Gotthelf answered 3/4, 2013 at 6:30 Comment(0)
D
6

If you want the JSON of a good quality profile picture with the URL you can use that:

http://graph.facebook.com/517267866/picture?height=1024&redirect=false

if you just need the picture use it without the parameter redirect:

http://graph.facebook.com/517267866/picture?height=1024

517267866 is the profile ID of one of the above examples. Put the facebook id that you need

I hope that helps

Dierdredieresis answered 18/1, 2017 at 9:22 Comment(2)
Not sure if this works for every Graph image objects. In some case for the url https://graph.facebook.com/XXXXX/picture?height=1024 the browser will download the image instead of displaying it. Any idea why?Palmieri
I think that depends on the browser you are using. However, you can use the facebook image url in any image component of your framework (IOS, Android, HTTP Image...)Dierdredieresis
V
3
$url = 'http://graph.facebook.com/100000771470028/picture?type=large';
$rray=get_headers($url);
$hd = $rray[4];
echo(substr($hd,strpos($hd,'http')));

This will return the url that you asked, and the problem of changing the url by facebook doesn't matter because you are dynamically calling the url from the original url.

Virago answered 13/7, 2011 at 11:31 Comment(0)
D
2
function getFacebookImageFromURL($url)
{
  $headers = get_headers($url, 1);
  if (isset($headers['Location']))
  {
    return $headers['Location'];
  }
}

$url = 'https://graph.facebook.com/zuck/picture?type=large';
$imageURL = getFacebookImageFromURL($url);
Demise answered 4/12, 2012 at 2:52 Comment(2)
picture?type=large was the only answer I needed here - thank you.Unbuild
This was 'god send'. I've been trying to find how to do this for days, then I found the answer here. Thanks @DemiseArthromere
B
2

this is the only one that really works:

me?fields=picture.type(*YOURTYPE*)

where YOURTYPE can be one of the following: small, normal, album, large, square

Bagman answered 19/10, 2015 at 9:19 Comment(0)
U
2

For Android:

According to latest Facebook SDK,

First you need to call GraphRequest API for getting all the details of user in which API also gives URL of current Profile Picture.

Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(token, "me", params, HttpMethod.GET,
        new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
                if (response != null) {
                    try {
                        JSONObject data = response.getJSONObject();
                        if (data.has("picture")) {
                            String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
}).executeAsync();
Upheld answered 26/11, 2015 at 10:47 Comment(0)
H
1

Now , Facebook need SSL

-> Important added S, https -> https://graph.facebook.com/userId/?fields=picture&type=large

Works In June / 2014

Holbein answered 30/6, 2014 at 18:55 Comment(0)
S
0

Hmm..i tried everything to get url to user image.The perfect solution was fql use like this->

    $fql_b = 'SELECT pic from user where uid = ' . $user_id;
    $ret_obj_b = $facebook->api(array(
                               'method' => 'fql.query',
                               'query' => $fql_b,
                             ));


             $dp_url =$ret_obj_b[0]['pic'];

replace pic by big,pic_square to get other desired results. Hope IT HELPED....

Sorcim answered 3/7, 2012 at 19:13 Comment(0)
C
0
ImageView user_picture;
userpicture=(ImageView)findViewById(R.id.userpicture);
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
userpicture.setImageBitmap(mIcon1);

Where ID is one your profile ID.

Crookback answered 18/5, 2015 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.