50

With the upgrade from Facebook Graph API v1.0 to v2.0, Facebook is using "App-scoped User IDs", so I cannot see the "original" user ID inside my apps anymore.

I always used the Real Time Updates and graph API calls like "https://graph.facebook.com/{postId}/comments" to analyze user activity on the Facebook page where the app was. But since these data contain the original user ID, I'm not able to match the activities to my registered users anymore!

So is there a way to get the original Facebook user ID from an app-scoped user ID? Or the other way round?

EDIT:

I ended up fetching the app-scoped IDs for all my users using the API method mentioned here: https://stackoverflow.com/a/29154912/3432305 and stored them in my database in addition to the old ID. Then, whenever an RTU arrived, for "old users" I would check which user has this app-scoped ID and process the update. At least Facebook managed to fix the bug mentioned in the comments (Get Facebook User ID from app-scoped User ID) a few days before the API v1.0 was deprecated, so for new users of my apps, it works like a charm. So far...

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
kolli
  • 1,260
  • 2
  • 13
  • 23
  • 2
    You should not have the problem because Graph API v2.0 is backward-compatible, if the user already authorized your app before, then you always get original user ID for this user. And if the user is new to authorized your app now, then you always get app_scoped_id instead. https://graph.facebook.com/{postId}/comments may return app-scoped user ID OR original user ID depends on this user is old user or not. – 林果皞 May 23 '14 at 09:06
  • Yes, I know about the backward compatibility. The actual problem is the Real Time Update. There I only have the "original" user ID... – kolli May 23 '14 at 12:31
  • 1
    There's an open bug about realtime updates using the wrong ID here: https://developers.facebook.com/bugs/744639905588610 – Igy May 29 '14 at 16:50
  • I would love to see that bug fixed, that would make things a lot easier for me :) thx for the info! – kolli Jun 05 '14 at 14:21
  • just to mention, https://www.facebook.com/app_scoped_user_id/264254410443692/ this type of urls work in the case you want to access a user profile from app scoped user id – Bill'o Aug 03 '15 at 15:46
  • If you are trying to determine whether an id is app scoped, user ids fall in the range [(id < 2200000000 || (id >= 100000000000000 && id <= 100099999989999)](https://github.com/facebookarchive/facebook-js-sdk/blob/deprecated/src/xfbml/helper.js#L37). App scoped user ids will fall outside of this range – Matthew Kolb Sep 28 '20 at 21:08

5 Answers5

30

From your conversation between Johannes N.

Can I get the app-scoped ids (for apps I own) via the original user id. that would help too.

To get app-scoped ids from original user id can be easily done via:

https://graph.facebook.com/v2.0/?ids=http://www.facebook.com/USER_ID&access_token=ACCESS_TOKEN

enter image description here

Yo can use fields parameter to return only id:

https://graph.facebook.com/v2.0/?ids=http://www.facebook.com/USER_ID&fields=id&access_token=ACCESS_TOKEN

enter image description here

The benefit of ids instead of id because you can query multiple ids:

enter image description here *www can be remove to minimize characters count.

To get app-scoped ids from username can be easily done too:

https://graph.facebook.com/v2.0/?ids=http://www.facebook.com/USERNAME&access_token=ACCESS_TOKEN

enter image description here

林果皞
  • 7,539
  • 3
  • 55
  • 70
  • Cool! I would really prefer to get the RTU with the app-scoped IDs - but if this is not possible, your solution will help me a lot! I would vote this up if I could.. ;) – kolli Jun 05 '14 at 14:14
  • 32
    Doesn’t seem to work anymore. Trying Facebook Graph Explorer with the user ID of 4 as above returns `"id": "http://www.facebook.com/4”` now. – Dylan Jul 03 '14 at 01:40
  • I suppose i will track down all my old users with name to find out real ID. – Yevgen Mar 02 '15 at 19:10
14

there's no (simple/obvious) possibility to get a real user id based on an app-scoped user id.

Johannes N.
  • 2,364
  • 3
  • 28
  • 45
  • I thought so... :( And the other way round? Can I get the app-scoped ids (for apps I own) via the original user id. that would help too. I think the Business Mapping API (https://developers.facebook.com/docs/apps/for-business) may offer something like this.. – kolli May 22 '14 at 13:38
  • btw: I would take a complicated/non-obvious solution too ;) otherwise the main functions of my apps would be pretty useless... – kolli May 22 '14 at 13:47
  • Okay @kolli here is a complicated solution: why not have an automated browser log in through your app and facebook oauth, and proceed to load all the people’s names and photos, and then scrape that and later schedule many fb searches by name for that photo? Then it can reconstruct it from the search LOL – Gregory Magarshak Aug 13 '20 at 03:02
8

You can't go from scoped back to real. Your best bet is converting all of them to scoped. It's pretty easy:

https://graph.facebook.com/?ids=REAL_ID&access_token=APP_ID|APP_SECRET

Here's some simple Ruby code that does this:

require 'open-uri'
require 'json'

json = JSON.load(open("https://graph.facebook.com/?ids=#{real_id}&access_token=#{FACEBOOK_APP_ID}|#{FACEBOOK_APP_SECRET}"))
scoped_id = /app_scoped_user_id\/(\d+)\//.match(json[real_id.to_s]['link'])[1]
Sam Soffes
  • 14,831
  • 9
  • 76
  • 80
  • 4
    Upvote for the idea, but it won't solve my problem unfortunately... For users that are already registered, I will still get the "real" ID when they log into the app. In fact, thats my main problem now - having users with "real" IDs as well as users with app-scoped IDs.. And RTUs will only have one of them.. – kolli Apr 15 '15 at 23:04
  • @Taha I didn't use this in the end, so no idea... Maybe Sam can help? – kolli Sep 28 '16 at 10:07
  • Haven't used this since after months after posted this answer. Best of luck! – Sam Soffes Oct 04 '16 at 13:49
-4

I found a website can help you get facebook user id from app scoped id.

It's here http://izitools.com/en/tool/get-facebook-id-from-scoped-id

enter image description here

API

enter image description here

Community
  • 1
  • 1
manhhaiphp
  • 123
  • 7
  • 1
    obsolete, not working anymore. Probably because API used older, deprecated version of Graph API – Ognj3n Oct 31 '16 at 13:43
  • 3
    Unfortunately the intended use of scoped IDs is to make it impossible to get the real ID from it. See my answer for more on this. – Sam Soffes May 11 '17 at 04:35
-7

there is a dirty hack to retrieve original user id

http://graph.facebook.com/app-scoped-id this returns the facebook public information as follows, { "id": "app-scoped-id", "first_name": "xxxx", "gender": "male", "last_name": "yyyy", "link": "https://www.facebook.com/zzzz", "locale": "en_US", "name": "xxxx yyyy", "username": "zzzz" }

Then you make another graph api call with username http://graph.facebook.com/zzzz

This will returns the original facebook id and other public information, instead of app-scoped-id

  • 2
    Username doesn't exists in API v2.0. And if you are using API v1.0 from the beginning you don't need to use app scoped user ids. – WizKid May 22 '14 at 16:08
  • 1
    Unfortunately, this doesn't work for me. I see all the fields but not the 'username' field... Also, when I try the second part with any user, I get an OAuthException "(#803) Cannot query users by their username (username)".. – kolli May 22 '14 at 16:13
  • As I said. Username is not available in API v2.0. – WizKid May 22 '14 at 16:15
  • Hm, I wonder if v1.0 even works for app-scoped IDs?... Graph Explorer won't let me try it, so I will test it from the app directly during the registration process.. Nevertheless, that would only help me as long as v1.0 is available... – kolli May 22 '14 at 16:48