Get username field in Facebook Graph API 2.0

后端 未结 8 510
忘了有多久
忘了有多久 2020-11-29 02:55

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 resul

相关标签:
8条回答
  • 2020-11-29 03:16

    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)
    
    0 讨论(0)
  • 2020-11-29 03:26

    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.

    0 讨论(0)
提交回复
热议问题