How to get user's username in v2.0 or later of Facebook's Graph API
Asked Answered
B

3

35

I used to get the user's username in the API 1.0 fairly easily, using /me and getting the username property of the response object.

Now I'm getting this error with API 2.0:

"(#12) username is deprecated for versions v2.0 and higher"

The only way I found to get this until now was to use FQL, but now it seems deprecated.

Is there a way around this?

Bosomy answered 4/5, 2014 at 12:30 Comment(1)
possible duplicate of Get username field in Facebook Graph API 2.0Empennage
V
23

I don't mean to be unhelpful, but it appears access to username has been removed from the API, as far as I can tell. Places where an app may have been using username, such as in the old share dialogs, can no longer do that when used with the 2.0 API. I think its also a way of preventing apps from having access to usable unique identifiers outside of the app scope - any user IDs you retrieve under 2.0 API are specific to your app alone.

Venosity answered 7/5, 2014 at 20:23 Comment(4)
That's my problem. I'm wanting to identify users, in my own database, that don't use my app yet. Like I have a bunch of records in the people table, each having a Facebook's username. So, when they log into my app, I would check if there was a person with that particular username, and log him in. As you can see, I cannot use the app_scopped_id because those users may have never used my app (but they would have access when they do). Prior to API 2.0, I was using the global user ID, which is not an option anymore as well. Anyway, thanks for your comment.Bosomy
The latest FB API got a class named "Profile" which can get everything you need.Scintillometer
The user_id and name attributes of the FBSDKProfile class in the 4.0 API are not what used to be known as "username". "name" is just the person's full name, combining first and last name, etc. user_id is the app scoped ID, not a universal ID. The choice to deprecate universal ID and username access by apps was a privacy choice - unlikely we'll ever have access to them. Best idea is the Business Mapping API detailed below.Venosity
This seems so incredibly backward. email is still available via permission, and access to that is far more potentially disruptive/invasive than a publicly available username/ID. It would seem at the very least that these properties would be available w/ a permission setting.Cyrenaic
G
8

I found a simple workaround that involves a get request to Facebook. Instead of the username, Facebook will give you an ID that is unique to your application.

I have found that making a request to https://www.facebook.com/[profile_id] will then redirect to the user's real profile. The username can be extracted from the redirect URL.

Example:

> curl -i https://www.facebook.com/710290539
HTTP/1.1 301 Moved Permanently
Location: https://www.facebook.com/colinskow

(Note: Since I am the owner of the app in test mode, this could possibly be an exception. Please let me know in comments if you are able to confirm this in a production environment.)

Groom answered 20/3, 2015 at 12:11 Comment(7)
Looks like Facebook has already changed that response. Now it will redirect to login before you access that url.Obryant
@Obryant I don't see any change in that response, still getting redirected to the username. Are you getting a different response?Ethbinium
I'm also being redirect to login page. @Ethbinium did you try accessing this page without being signed in?Nashom
@Nashom I did. It worked, but only for a while.Ethbinium
you need to be logged in and then it will work, but how do I login first?Vociferate
It is still working for me using the CURL command line without being logged in. This means it should work using any HTTP Request API.Groom
It worked for older ids for me and requires login for newer ids.Fullmouthed
C
5

As a workaround you can use the email as a unique identifier. Email address can be retrieved using "email" as the permission scope.

Facebook has removed the username field from the new API version. It is not possible to retrieve the username. But Facebook provides an application specific unique ID. If you need to share the same user between several apps you can use the newly introduced Business Mapping API. This allows to add all the required apps to a group. In this case the ID will be unique among all the apps in the group.

More information on Business Mapping API is available at https://developers.facebook.com/docs/apps/for-business 1

Clarino answered 16/6, 2014 at 8:25 Comment(5)
My problem is that I need something to identify users that didn't even start to use my system. Like, the system admin would enter "something" that identifies the user in facebook, and, when an user tries to log into my system, he would be granted access if this "something" matches with a record in my database. Using the email WOULD work indeed, but do you know how to get any user's email, if it is not yet using my system, that could be entered by the system admin? I previously used the username because it is already in the user's profile URL, and it was so easy.Bosomy
Using the email isn't reliable. It seems that if a user hides their Facebook email from their timeline, as I've done, Facebook won't return it.Euchologion
are you sure app scoped user ids are same in all apps which was added in FB business manager?? when i call this /me/ids_for_business by graph api i see user id is different per app. please answer.Ebb
Not all facebook users have emails, many of them registered by phoneFullmouthed
Late comment, but still... what I do, is this: 1) I create an '1-time access token' for the user ID 2) I send that token to the user, via email, as a URL 3) The user clicks on it, opens a page. The user is already logged in onto the application 4) I ask the user to run Facebook authentication, and when the callback gets called, I assign that (scoped, app-wide) facebook ID to that particular user. Note that in (4), I will check if the user ID is already "taken". I have working Node code for this, using node's Passport if you are interestedBloomy

© 2022 - 2024 — McMap. All rights reserved.