59

The "old" Facebook Graph API had a "username" field which could be used to create a human-readable profile URL. My username for example is "sebastian.trug" which results in a Facebook profile URL http://www.facebook.com/sebastian.trug.

With Graph API 2.0 Facebook has removed the "username" field from the user data as retrieved from "/me".

Is there any way to get this data via the 2.0 API or is the "username" now being treated as a deprecated field?

trueg
  • 601
  • 1
  • 5
  • 4

8 Answers8

32

Facebook got rid of the username because the username is one way of sending emails via Facebook.

For example, given the url http://www.facebook.com/sebastian.trug

the corresponding Facebook email would be sebastian.trug@facebook.com

which, if emailed, would be received to messages directly (if the message setting is set to public), otherwise to the other inbox.

user664833
  • 18,397
  • 19
  • 91
  • 140
Nico Zarris
  • 362
  • 5
  • 10
26

The username field of the User object has been removed, and does not exist in Graph API v2.0. In v2.0 of the API is there is no way to get the FB username of a user.

Source: https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api

"/me/username is no longer available."

Simon Cross
  • 13,315
  • 3
  • 32
  • 26
19

@Simon Cross - It being deprecated is documented, yes. That is not the question, the question is how to get it and -furthermore- I wonder why Facebook has made such a terrible choice and removed the username. Hundreds of applications that rely on the username to create accounts on their service will be broken.

@user3596238 - You can stick with the V.1 API which will be around until the end of April 2015, by far not the best solution but Facebook might be irrelevant by then anyway. https://developers.facebook.com/docs/apps/changelog

Solution: ask the user for a username besides the actual Facebook login? - In my opinion, that makes the Facebook login completely pointless anyway.

Stefan
  • 1,214
  • 1
  • 9
  • 17
  • 3
    Why in hell would they remove the username? As you say it makes it completely pointless now. The issue here is I cannot go back to the old API as Apple is rejecting apps due to "Improper Advertising Identifier [IDFA] Usage" from the AdSupport framework required by Facebook old SDK – Lukas May 27 '14 at 08:16
  • 2
    @pechar, I think the best solution for you would be to just generate a username on the fly using the user's first name and the FB user ID. You will have something like James1636617726627162 but at least you can overcome the issue like that. – Stefan Jun 19 '14 at 20:59
  • Yes as you say it makes sense though I will have to turn to a more user customizable username as it is used publicly. Thanks for your tip. Just a side note the FB user ID is now per app not per user if the user has never logged in using the new SDK. – Lukas Jun 20 '14 at 06:29
  • The reason Facebook removed username is that it is a unique identifier for a given Facebook user. In v2.0, to help protect people's information, Facebook moved to emit app-scoped user IDs for people who login to your app. By definition, these give you an app-scoped identifier for a user. Emitting the username would break this paradigm, so it had to be removed in v2.0. If you want to create a username for a user in your app based on their FB credentials, I recommend lowercasing and concatenating their first and last names. – Simon Cross Jun 26 '14 at 19:58
  • 2
    FB; we removed the user ID since it is a unique identifier and we aim to protect your data. Oh well, actually, we are making huge amounts of money with your data but we still want to keep you under the impression that you are safe with us. – Stefan Aug 28 '14 at 11:51
  • 2
    @SimonCross lower-cased first + second name is not unique and adding numbers at the end makes identifier ugly. – Adrian Kalbarczyk Jan 04 '15 at 21:14
  • @AdrianKalbarczyk then i recommend you ask the user to enter a username - if one is really needed at all. – Simon Cross Jan 11 '15 at 18:41
  • @SimonCross What is FB signup for then?:) We've decided to go with our own registration system based on e-mail address and then option to integrate FB/Twitter/etc. It's much more transparent to our users. – Adrian Kalbarczyk Jan 12 '15 at 20:02
  • You can use FB as a sign in system without needing a username - it's a product decision on your end to require username. – Simon Cross Jan 16 '15 at 21:49
  • 6
    " but Facebook might be irrelevant by then anyway." – Kraken Apr 18 '17 at 17:14
7

While the 2.0 SDK will not provide the username field any longer, it is quite easily scraped if you have the user id number (which you will likely be using to access the graph anyway).

The url facebook.com/<user id> will redirect to facebook.com/<username>, which can then be extracted however you like.

Matt Way
  • 32,319
  • 10
  • 79
  • 85
  • 11
    This approach will not work anymore; Facebook now gives you an app-scoped version of the user ID which will only redirect to /username if you are already logged in. – kylewm Mar 09 '16 at 16:35
5

my approach is scrapping the username using nokogiri through the user profile. kinda like this (in ruby):

html = RestClient.get("http://facebook.com/123123xxx)
doc = Nokogiri::HTML(html)
username = doc.css('head meta')[1].attributes["content"].value
kubido
  • 546
  • 6
  • 11
2

One of the ways could be to access facebook.com/{userid} using cURL and then follow the redirect.

The page rediects to facebook.com/{username}

Akshat Goel
  • 736
  • 2
  • 6
  • 19
-4

Inspired from @RifkiFauzi 's answer, here is my solution in pure Python

#get html of a page via pure python ref. https://stackoverflow.com/a/23565355/248616
import requests
r = requests.get('http://fb.com/%s' % FB_USER_ID) #open profile page of the facebook user
r.raise_for_status()
html = r.content

#search string with regex ref. https://stackoverflow.com/a/4667014/248616
import re
# m = re.search('meta http-equiv="refresh" content="0; URL=/([^?]+)\?', html)
m = re.search('a class="profileLink" href="([^"]+)"', html)
href = m.group(1) #will be https://www.facebook.com/$FB_USER_NAME on 201705.24
username = href.split('/')[-1]
print(href)
print(username)
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
-8
https://graph.facebook.com/?id=100005908663675

Simply change id to whatever.

user3788486
  • 88
  • 11
  • that only works with your facebook id, which is not accessible from the api since every id is app specific: https://developers.facebook.com/docs/graph-api/reference/v2.2/user – kilianc Nov 05 '14 at 21:03