0

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.

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
user30102967
  • 221
  • 2
  • 13
  • @YvesLeBorg in that case OPs questions is offtopic as it's asking of off page software. – DocRattie Aug 04 '15 at 11:50
  • 3
    @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. Aug 04 '15 at 11:56
  • @V.J. I neither try to be rude nor to be offensive. But it's offtopic to ask for off page software on SO. – DocRattie Aug 04 '15 at 12:11
  • He is asking that is it possible or not. Your answer must be like this. "No, this is not possible". Simple. – V.J. Aug 04 '15 at 12:17
  • 3
    @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 Aug 04 '15 at 12:21
  • @YvesLeBorg dude, I didn't delete any comments. But maybe you flaged it and it got deleted due to that. – DocRattie Aug 04 '15 at 12:25
  • @V.J. Does he at any point ask "If it's possible" ? They way I understand it he stat's that it's possible and that there is a page that does it. Therefore I understand it the way that OP want's to know "How it's possible" and that means asking for their code. – DocRattie Aug 04 '15 at 12:27
  • 1
    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. Aug 04 '15 at 12:36
  • 1
    @V.J. you will get a hint if you show `source` in your developer tools. – YvesLeBorg Aug 04 '15 at 12:50
  • 4
    Yes i got the hint. Open https://facebook.com/username in browser. see the html code. find "" this tag. It is with all the facebook username. – V.J. Aug 04 '15 at 12:58
  • That's great guys, that's what i was looking for, i thought of the source code first but never knew it might show in the source code. – user30102967 Aug 04 '15 at 13:09
  • @user3010296 i have posted the answer to find the userid from username without using api. Please check it. Its a c# code. – V.J. Aug 04 '15 at 13:22
  • 1
    @YvesLeBorg Check my answer. I did it. Not 100%. But 90-92%. – V.J. Aug 04 '15 at 13:29

1 Answers1

4

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.
V.J.
  • 9,492
  • 4
  • 33
  • 49
  • 1
    Ok. After that please post your ans for the PHP. – V.J. Aug 04 '15 at 13:25
  • I am almost done with the code. But i am not able to right the code that matches the pattern : using preg_match_all(); it needs to be something like this : // preg_match_all('', $like_html, $matches); – user30102967 Aug 04 '15 at 14:12
  • I am not familiar with php. So not able to help you in this case. Sorry dude. – V.J. Aug 05 '15 at 05:18