Get username field in Facebook Graph API 2.0

2019-01-13 09:35发布

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?

8条回答
三岁会撩人
2楼-- · 2019-01-13 09:57

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)
查看更多
甜甜的少女心
3楼-- · 2019-01-13 09:58
https://graph.facebook.com/?id=100005908663675

Simply change id to whatever.

查看更多
Deceive 欺骗
4楼-- · 2019-01-13 10:03

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

查看更多
来,给爷笑一个
5楼-- · 2019-01-13 10:06

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}

查看更多
做个烂人
6楼-- · 2019-01-13 10:12

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
查看更多
女痞
7楼-- · 2019-01-13 10:14

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.

查看更多
登录 后发表回答