0
votes

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
@DocRattie He is just asking a question. If you don't have the ans then leave it. Don't try to be "rude or offensive". - V.J.
@DocRattie : way to go !!! deleting the rude and offensive comment ! A bit of common-sense with the 'rules and regs' and courtesy go a long way towards the value of SO. The question is valid IMO because every other answer here states it cant be done, yet this website succeeds ! - YvesLeBorg
I am dying to know that how that site makes it possible. Because facebook and its graph api does not provide anything to fetch userid from username. - V.J.
@V.J. you will get a hint if you show source in your developer tools. - YvesLeBorg
Yes i got the hint. Open facebook.com/username in browser. see the html code. find "<meta property="al:android:url" content="fb://profile/100006480074833">" this tag. It is with all the facebook username. - V.J.

1 Answers

4
votes

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.