Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 278
joaopribs Avatar asked May 04 '14 12:05

joaopribs


People also ask

How do I get my Facebook Graph API User ID?

The simplest way to get a copy of the User Profile object is to access the /me endpoint: FB. api('/me', function(response) { }); This will this will return the users name and an ID by default.

What user data can I get from Facebook API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

Is Facebook Graph API restful?

Because the Facebook Graph API is a restful JSON API, you can run queries directly in any standard browser; you'd simply be making HTTP calls.


2 Answers

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.

like image 150
giff Avatar answered Sep 20 '22 07:09

giff


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.)

like image 22
Colin Skow Avatar answered Sep 20 '22 07:09

Colin Skow