39

I have seen this question but what I want is different.

I want to get the Facebook ID not from a general URL (and therefore conditional if it has Like button or not). I want to get the Facebook ID given a Facebook page using the Graph API.

Notice that Facebook pages can have several formats, such as:

http://www.facebook.com/my_page_name
http://www.facebook.com/pages/my_page_name
http://www.facebook.com/my_page_ID

I know I could do some regex to get either the my_page name or my_page_ID, but I am wondering if any one know if GraphAPI is supporting what I want.

Community
  • 1
  • 1
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
  • 1
    It's worth noting that although the Facebook API specifies that pages are looked up via {page-id} and that the ID is a "numeric string", the text name seems to work equally well. – Kylotan Jun 23 '15 at 19:11

9 Answers9

31

It seems to me that the easiest solution to what you describe is to just get the id/name from the url you have using lastIndexOf("/") (which most languages have an equivalent for) and then get "https://graph.facebook.com/" + id.

The data that this url returns has the id (i.e.: 6708787004) and the username (i.e.: southpark), so regardless of which identifier you use (what you extract from the url using lastIndexOf), you should get the same result.


Edit

This code:

identifier = url.substring(url.lastIndexOf("/"))
graphUrl = "https://graph.facebook.com/" + identifier
urlJsonData = getGraphData(graphUrl)

Should work the same (that is result with the same data) for both:

url = http://www.facebook.com/southpark

And

url = http://www.facebook.com/6708787004

(you'll obviously need to implement the getGraphData method).

Also, the 2nd url form in the question is not a valid url for pages, at least not from my tests, I get:

You may have clicked an expired link or mistyped the address. Some web addresses are case sensitive.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • can you please explain this more this is also useful for me i understand what you mean but i didn't get the id or name using lastindexof() can you please give the complete code – Abdullah Adam Apr 14 '12 at 07:32
  • 2
    This doesn't always work. I'm looking at a page whose URL has the form http://www.facebook.com/pages//. I tried asking for graph.facebook.com/, but I get an error: { "error": { "message": "Unsupported get request.", "type": "GraphMethodException", "code": 100 } }. Using the whole path doesn't work either. This is an unpublished page created by a test user (I'm trying to test an app that needs to get page ids programmatically). – Chris Westin Dec 14 '12 at 23:53
  • @NitzanTomer This works: graph.facebook.com/ – bart Jan 12 '14 at 20:54
  • @NitzanTomer This code won't work if the URL ends with a trailing slash. Which the event URLs in Facebook all seem to have now. – Jon Koops Mar 24 '15 at 23:08
  • 4
    This doesn't work anymore ? If you go to http://graph.facebook.com/my_name, it says it requires an access token. – Link14 Oct 20 '15 at 00:10
  • This method will work with a short-term user access token, which has been granted interactively, but won't work using an app access token or a test user access token. – amoe Feb 01 '17 at 11:21
  • Both works for me. But the reason to get fb id rather than name because the name can be changed. If I store "southpark" in my database later the owner change it to "southparkOfficial", I can't get to access this page using fb name "southpark". – errorare Feb 20 '17 at 06:53
10

The answer to the question is posted above but the method shown below works fine we do not have to perform the regex on the facebook page urls

I got the answer by this method

FB.api('/any_fb_page_url', function(response){
  console.log(response);
}); 

any_fb_page_url can be any of the following types

https://www.facebook.com/my_page_name
https://www.facebook.com/pages/my_page_name
https://www.facebook.com/my_page_ID

This are also listed in question above

This code is tested on JS console available on Facebook Developers site tools

ankhzet
  • 2,517
  • 1
  • 24
  • 31
Suraj Palwe
  • 2,080
  • 3
  • 26
  • 42
6

You can get the page id by using the below api

https://graph.facebook.com/v2.7/smhackapp?fields=id,name,fan_count,picture,is_verified&access_token=access_token&format=json

Reference image

Mani
  • 79
  • 1
  • 1
6

This answer is updated and checked in 2019: and it is very simple because you do not need to extract anything from the link. for examples:

  1. https://www.facebook.com/pg/Vaireo-Shop-2138395226250622/about/
  2. https://www.facebook.com/withminta
  3. https://www.facebook.com/2138395226250622

https://graph.facebook.com/?id=link&access_token=xxxxxxxx

response: { "name": "Vaireo Shop", "id": "2138395226250622" }

full nodeJS answer:

async function getBusinessFromFBByPageURL(pageURL: string) {
    const accessToken = process.env.fb_app_access_token;
    const graphUrl = `https://graph.facebook.com/?id=${pageURL}? access_token=${accessToken}`;
    const fbGraphResponse = await Axios.get(graphUrl);
dang
  • 1,549
  • 1
  • 20
  • 25
  • Can any Facebook URL be passed directly into the API and that will return a profile object? – Lobstw Jul 26 '19 at 15:24
5
<?php

function getFacebookId($url) {

    $id =  substr(strrchr($url,'/'),1); 

    $json = file_get_contents('http://graph.facebook.com/'.$id);

    $json = json_decode($json);

    return $json->id;

}


echo getFacebookId($_GET['url']);


?>

Thats a PHP example of how to get the ID.

Alon Carmel
  • 660
  • 1
  • 6
  • 16
  • 1
    How do you know that you will always get url having facebook page id. There might be page name instead of page id. What will you do in that case? – Maha Dev Dec 05 '15 at 08:13
1

As of Nov 26 2021 none of these solutions work. Facebook has locked down the API so you need an App Review.

https://developers.facebook.com/docs/pages/overview/permissions-features#features

Overload119
  • 5,124
  • 5
  • 29
  • 34
0

This answer takes into account that a URL can end with a trailing slash, something that Facebook event pages seem to have in their URLs now.

function getId(url) {
  var path = new URL(url).pathname;
  var parts = path.split('/');

  parts = parts.filter(function(part) {
    return part.length !== 0;
  });

  return parts[parts.length - 1];
}
Jon Koops
  • 8,801
  • 6
  • 29
  • 51
0

You can Use Requests and re Modules in python

Code:

import requests,re

profile_url = "https://www.facebook.com/alanwalker97"

idre = re.complie('"entity_id":"([0-9]+)"')

con = requests.get(profile_url).content

id = idre.findall(con)

print("\n[*] ID: "+id[0])

Output:

[*] ID: 100001013078780

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ahmed
  • 414
  • 4
  • 3
-1

Perhaps you can look through the https://developers.facebook.com/docs/reference/api/#searching docs: search against a couple of types and if you find what you're looking for go from there.

Femi
  • 64,273
  • 8
  • 118
  • 148