Dhiraj
Dhiraj

Reputation: 1119

Facebook Graph Api 2.1 get userid from username

I am looking for a solution which can provide me user id from profile url or fb username

example :http://facebook.com/username

get userid of this url

and also when I try to read friends (I have friend related permission), I am getting blank data array ex:

 graph api requ: https://graph.facebook.com/v2.1/me/friends?access_token=xxxxx
 response:
    {
      "data": [
      ], 
      "summary": {
        "total_count": 455
      }
    }

Please let me know how can I get friend list and userid using username in Facebook graph api v2.1

Upvotes: 12

Views: 30897

Answers (6)

Mr.Cat
Mr.Cat

Reputation: 330

You can send HTTP request to the facebook profile page from your server-side code
i.e. request to https://facebook.com/some_username
Parse the received response containing HTML source code and search for a <meta> tag that has a content attribute with a value looking like this fb://profile/xxx where xxx is going to be the profile ID you're looking for.

#Update (May, 2020):
Facebook has recently switched to a new web interface and the meta tag I mentioned above is found only in the older version.
In the new Facebook version you can find the profile ID in many places but All I found so far were inside the body of various <script> tags.
You may parse any script body that starts with requireLazy and look for

"result":{"data":{"user":{"id":"xxx"}

Or

"userID":"xxx"

Upvotes: 3

Ray
Ray

Reputation: 41508

I've found if you go to a username's page like:

https://www.facebook.com/username

If you right click on the user's photo, copy the image link you'll get something like :

https://www.facebook.com/photo.php?fbid=12332322&set=a.1afs2&type=3&source=11&referrer_profile_id=100024670088999

The referrer_profile_id is the user's facebook id. Not much help from an API perspective, but gets you the id.

Upvotes: 1

az_
az_

Reputation: 1503

Duplicate: api to get uid from profile URL on facebook

You can call the graph directly as such: https://graph.facebook.com/username where username is the username you want to lookup.

edit: out of date

Upvotes: -3

user1857742
user1857742

Reputation: 151

I have also noticed that the JSON from the /me/home node gives user names and their ids from those people who liked anything. Since these people may also be friends, you might be able to partially figure out user ids for friends by looking at this feed in combination with taggable_friends. I am not sure what the Facebook (v2.3) terms of use says in this regard -- so maybe this isn't allowed?

Upvotes: 0

pndfam05
pndfam05

Reputation: 171

graph.facebook.com/username has stopped working. There is an unpleasant workaround. In my limited testing, this works in Chrome.

Wherever you see the facebook user name in a timeline entry, right click and select "inspect element." In the window that opens, scroll up a little and look for the string:

"data-hovercard="/ajax/hovercard/hovercard.php?id=".

The Facebook id of the username you right clicked on follows the = sign.

This only seems to work on names on the timeline.

Upvotes: 7

Syed Abuturab
Syed Abuturab

Reputation: 150

Hi dhiraj you can get all of your friends username, user id and their profile picture by doing this....https://graph.facebook.com/v2.1/me/taggable_friends?access_token=xxxxx....Hope this helps...and please dont forget to upvote if you identify this as an answer

https://graph.facebook.com/v2.1/me/friends?access_token=xxxxx would return the friends who are using your app i.e., who have authenticated your app...where as https://graph.facebook.com/v2.1/me/taggable_friends?access_token=xxxxx will return all of your friends

Upvotes: 3

Related Questions