Unable to access user's username using Facebook api 2.0

3.7k Views Asked by At

I am using facebook api 2.0 to allow users to use their Facebook profile data to fill up my custom form. (I don't want facebook's prefilled form). I am getting other information like first name, last name, email. But apparently Facebook api 2.0 has removed username from response object and I am unable to access it. I have observed that Response.link contains url of the person's profile and the portion after "www.facebook.com/" is identical to username. If we succeed in fetching link, we could get Username. But I am not sure this is true for all users. However when i try to access the link, I get link with app scoped id like this:

"www.facebook.com/app_scoped_user_id/XXXXXX/"  

where last portion (XXXXXX) is some numerical id. Does anybody know how to access the link in the form of "https://www.facebook.com/username"?

Also, if we are not able to do that in 2.0 but we can in 1.0, then how to switch back to 1.0?

4

There are 4 best solutions below

6
On

As https://developers.facebook.com/docs/apps/changelog#v2_0_api_versions is clearly stating

/me/username is no longer available

If your app is created after April 30th 2014, there's no way switching back to v1.0. If not, you can prepend /v1.0 to your request, but that will only work until April 30th, 2015.

0
On

Note that username is optional on Facebook. By default a Facebook profile doesn't have a username

enter image description here

In any Facebook API response, you'll get the username, only if the user has set his username under his Facebook settings.

2
On

If you only require current authorized user's username(or any other authorized user for your app), then it's possible.

First step, you call /v2.0/me?fields=third_party_id endpoint . You have to use authorized User Access Token (Authorized with basic scope, i.e. public_profile permission enough).

enter image description here

Or alternative way, you can use App Access Token instead. Of course you can't use me connection anymore, you have to explicitly to put User ID. For example, 12345678 shown on the folowing screenshot:

enter image description here

Second step, use App Access Token (User Access Token is not allow here) to call following FQL query:

SELECT username FROM standard_user_info WHERE third_party_id = 'example12345abcdef'

In which "example12345abcdef" is the third_party_id example you get from the first step.

enter image description here

p/s: please note that the user/app access token on first step must use the same app on second step. It's because third_party_id is tied to app.

Cheers :)

0
On

If you just need to show the user his name why not use the "name" field? It contains their first and last name both. I have changed my app to use name instead of "username".