0

Is there a simple, reliable and potentially future-proof way to extract a graph ID from the API given any facebook URL? By "any facebook URL" I mean the URL for a personal profile, page, group, etc. All these things have various formats so I imagine there must be something in the graph API to definitively convert a facebook URL into an ID, right?

Steve
  • 8,066
  • 11
  • 70
  • 112
  • "simple, reliable and potentially future-proof" ROTFLMAO!! Seriously, with Facebook? I've been using Facebook's APIs for over four years now and the one thing you can count on is change, change for the sake of change, undocumented changes, changes done without any warning what-so-ever, pull-the-rug-from-under-you changes that affect paying customers, etc., etc. __You're just going to have to be diligent to stay on top of them and always check your code.__ @phwd has a great current solution for you. Use it and monitor it for changes. Happy coding! – DMCS Feb 10 '12 at 11:48
  • @DMCS Not constructive at all. I asked "is there", fully expecting to find out that there is no way, but hoping anyway. – Steve Feb 10 '12 at 13:04
  • At least you took my advice about phwd's answer. Just remember, stay on top of it as Facebook will change! – DMCS Feb 10 '12 at 13:32
  • 1
    Yes, that's why I pressed cancel when I was about to flag the comment as unconstructive. – Steve Feb 10 '12 at 17:20

2 Answers2

1

No, there isn't a way to do this simply within the API. You will need a set of pattern matching to match the various types of urls to extract either the id (album,note,photo,status) or username.

For example

Photos

Posts

Pages

  • facebook.com/pages/Joel-Spolsky/106083232757300
  • graph.facebook.com/106083232757300

Videos

  • facebook.com/video/video.php?v=10150398154330484
  • graph.facebook.com/0150398154330484

Events

  • facebook.com/events/138745136241434/
  • etc ...

Then it gets further complicated that even if you were able to get a silver bullet function that handles all these links your app would need to grant access to numerous permissions in order to access certain objects.

You may be able to get away with most links that have the id at the end but not all. So you can maybe use a regular expression catching links that end in numeric characters.

phwd
  • 19,975
  • 5
  • 50
  • 78
  • Fair enough. Might have to raise a bug. – Steve Feb 10 '12 at 13:05
  • singular links API is deprecated for versions v2.4 and higher http://stackoverflow.com/questions/31353591/how-should-we-retrieve-an-individual-post-now-that-post-id-is-deprecated-in-v – user1105951 Oct 27 '16 at 10:02
0

There is! You are looking for the object_url table, which you can query using FQL or directly with a request. See here: http://developers.facebook.com/docs/reference/fql/object_url/

EDIT:

You can also do this, but obviously less optimal:

function getObjectByUrl(url, cb) {
     FB.api("/" + url.replace(/^.*?www.facebook.com/, ""), cb);
}
Art
  • 5,864
  • 3
  • 30
  • 32
  • This doesn't work. For example, asking for [Zuck's ID](https://graph.facebook.com/http://www.facebook.com/zuck) doesn't return his actual ID. – josh3736 Feb 09 '12 at 23:21
  • @josh3736 You are right, haven't found a way to do that. However, edited to add the solution that I use. – Art Feb 09 '12 at 23:38
  • I am amazed there is not an easier way to get Graph ID. If you run this: https://graph.facebook.com/zuck it returns some kind of ID but it is not Graph ID. – Lee Woodman May 26 '15 at 07:34