Facebook graph API: ID of user profile picture

41,650

Solution 1

You can get the users public profile photo from the following url:

https://graph.facebook.com/[id]/picture

Reference: http://developers.facebook.com/docs/reference/api/

Solution 2

The answer seems to be yes

e.g., the photos in an album have an ID (the profile photo is a different object though, which a different FB ID). An ID for every object is a core concept of FB's new graph API: Every object in the social graph has a unique ID.

Indeed, data request i have ever made through the FB Graph API returns (when successful) a response in the form of a JSON array, comprised of nested ID-Value pairs, e.g. the education field from a User object:

"education": [
    {
       "school": {
           "id": "126533127390327",
           "name": "Massachusetts Institute of Technology"
    },
       "year": {
            "id": "140829239279495",
            "name": "1992"
    },
       "concentration": [
           {
                "id": "192578844099494",
                "name": "Computer Science"
           }
       ],
       "type": "College"
}

The profile photo is a connection of the User object (i.e., every FB object has Fields and Connections).

According to the table in the relevant FB Developer Page, a call to the Graph API requesting picture (user's profile photo(s)) returns a string which is the URL for the user's profile picture.

But why doesn't this same call return the user's profile photo ID?

The reason i suppose is that the URL returns:

Graph API : User Properties

The user's profile photo is not there, but in an adjacent node:

Graph API : User : Connections : picture

(See FB Documentation for Graph API structure here).

When you make that API call, examine the Request Headers, and in particular, the Request URL; when you do, you'll see something like this:

http://profile.ak.fbcdn.net/hprofile-ak-snc4/32093_100005743929541_5467982_q.jpg

The string between the underscores (100005743929541) is the user ID. this is easy to verify by making another call to the Graph API. From your browser location bar (assuming you are logged into Facebook) just enter:

https://graph.facebook.com/me

So again, the first item in that JSON string is the user's FB ID. Given that, it seems to me that if user profile ID does indeed have its own ID, then that string (user's FB ID) plus the two smaller adjacent strings of integers on either end of the ID, should be it--in other words, 32093_100005743929541_5467982 in the Request URL above.

Finally, perhaps the best way to answer this is by using the new Graph API Explorer. (I just tried to verify this, but my requests are hanging.)

Solution 3

There is one more simple way to accomplish this task.

  1. Fetch all facebook albums by https://graph.facebook.com/me/albums?access_token=.

  2. Loop into the albums and check if object type == 'profile'.

  3. Get the value of object cover_photo where type == 'profile'.

This value of cover_photo is the id of profile pic of the user.

You can check it by https://graph.facebook.com/<cover_photo value>?access_tokne=

Solution 4

I don't know if you still need this but this is the way I did it in PHP:

$albumsfb = $facebook->api('/me/albums/?fields=type');
$albumsfb = $albumsfb['data'];
foreach ($albumsfb as $albumfb) {
    if ($albumfb['type']=='profile'){
        $albumprofile = $albumfb['id'];
        break;
    };
}
$photoprofilefb = $facebook->api('/'.$albumprofile.'/?fields=cover_photo');
$photoprofilefb = $photoprofilefb['cover_photo'];
$photoprofilefb = $facebook->api('/'.$photoprofilefb);
$fotoperfilfburl = $photoprofilefb['source'];

Instead of "me", put the user id that you want?

Share:
41,650

Related videos on Youtube

JacquesB
Author by

JacquesB

Updated on July 13, 2020

Comments

  • JacquesB
    JacquesB almost 4 years

    Is there a way to get the ID of the picture that is the current profile picture for a user?

    Edit: OK, seems there is no trivial way to get the ID!

    However, it appears from random testing that every user has a single album with attribute type="profile", and the first image in this album is always the profile picture, so it is possible to get the ID of the profile picture that way. Can some more experienced with Facebook confirm that this is indeed always the case?

  • johnnymire
    johnnymire over 12 years
    "This value of cover_photo is the id of profile pic of the user." - That is not the case
  • johnnymire
    johnnymire over 12 years
    That doesn't give you the photo ID though
  • thdoan
    thdoan over 8 years
    It doesn't seem like you can reliably get the user ID from the profile picture anymore. Here's my user ID from /me: 10153540253304706. Here's the ID extracted from my profile picture URL: 10152550339904706. They don't match.
  • BlackOverlord
    BlackOverlord over 7 years
    I guess this is correct answer, becuse simple testing shows that cover of this album is always equals the profile picture.

Related