Retrieve facebook user id from the username

2019-09-21 07:18发布

I want to retrieve a Facebook user id if I have his username. This was deprecated from the graph API but I would like to know a work around on getting the id if i have the username.

There is a website that does that but I am not sure how they do it.

1条回答
我只想做你的唯一
2楼-- · 2019-09-21 08:04

I did the R&D and found that the given website is not using the API to fetch userid from the username. They are using another mechanism which is not valid.

I have found 1 mechanism to find the userid from the username.

Below is the c# code which identify how to find userid from username. I tried to read the HTML from facebook url. and trying to read below meta tag to identify userid.

META Code

<meta property="al:android:url" content="fb://profile/USERID">

Function

public string findUserIdfromUsername(string facebookURL)
{
    try
    {
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(facebookURL);

        List<String> keyLst = doc.DocumentNode
                                .SelectSingleNode("//meta[@property='al:android:url']")
                                .Attributes["content"].Value
                                .Split(',').ToList();


        foreach (string strkey in keyLst)
        {
            string[] arrTemp = strkey.Split('/');
            int arrlength = arrTemp.Count();
            string facebookUserID = arrlength > 0 ? (arrTemp[arrlength - 1]) : strkey;
            facebookUserID = facebookUserID.Replace("?id=", "");
            return facebookUserID;
        }
    }
    catch (Exception ex)
    { 

    }
    return "";
}

Requirement

  1. I have used "HtmlAgilityPack.dll" to load the html and parse it like XML.
  2. To test call the function like below. findUserIdfromUsername("https://facebook.com/USERNAME");
  3. I have only test 3-4 urls and getting right userid.
  4. I am not using any API to find USERID.
  5. My English and steps are not too much good. So please manage it.
查看更多
登录 后发表回答