Get Facebook User ID from app-scoped User ID - facebook

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...

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
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
The benefit of ids instead of id because you can query multiple ids:
*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

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

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]

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
API

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

Related

How to display "from" fields in comments via Facebook Graph API

I would like to find the id (app-scoped) for someone who has posted a comment on a public page.
For example, I get a list of recent posts/comments on the NY Times page via:
v2.11/nytimes/feed?fields=comments.limit(10){message,created_time,from},name,message,from&limit=2
The data returned looks like this:
"comments": {
"data": [
{
"message": "Wouldn’t know. Not paying $13/mo for this.",
"created_time": "2017-12-10T05:57:18+0000",
"id": "10151412260164999_10151414049324999"
}
],
There is no from field. The same is also true if I attempt to view the comment directly using it's id with
v2.11/10151412260164999_10151414049324999?fields=from,message,created_time
I have tried using the Facebook Graph API Explorer, using both my User Token, as well as an App Token.
Since v2.11 of the Graph API, you need a Page Token to get user data of comments: https://developers.facebook.com/docs/graph-api/changelog/version2.11#gapi-90
User information will not be included in GET responses for any objects owned by (on) a Page unless the request is made with a Page access token. This affects all nodes and edges that return data for objects owned by a Page.
In other words: You can only get user information if you manage the Page.
Edit: Since the latests update in the Graph API, you have to get the App reviewed and you must go through Business Verification or Individual Verification. (thanx #Guiman04)
Please check whether you are using the accessToken for the page you are trying to get data from, i had same issue, and was resolved by using the correct accessToken for the page.

Redirect to profile page using facebook api

I need to create module for website which display feeds from client.
I had get feed from facebook using graph api of fb. But when i click on the profile name it says profile is not available.
This issue is occuring because facebook has moved to graph api 2 and in this version user is returned on basis on app which is diffrent from actual user id.
So if i need to redirect page from the website. How can i implement this?
Thank you for your time.
You can use the link field of the User object like this:
/me?fields=id,first_name,last_name,link
The result will be similar to the following:
{
"id": "10152055263451244234",
"first_name": "TheFirstName",
"last_name": "TheLastName",
"link": "https://www.facebook.com/app_scoped_user_id/10152055263451244234/"
}
Or, if you already have a app-scoped user_id, you can construct the link manually by
"https://www.facebook.com/app_scoped_user_id/" + {user_id} + "/"

Facebook user url by id

I have a list of FB ids, is there a canon way of constructing their FB url without a graph query?
For example, I have ids 3, 4, 5, and i want the Facebook URL for them without using the graph api and save them down
UPDATE 2: This information is no more given by facebook. There is an official announcement for the behavior change (https://developers.facebook.com/blog/post/2018/04/19/facebook-login-changes-address-abuse/) but none for its alternative.
Yes, Just use this link and append your ID to the id parameter:
https://facebook.com/profile.php?id=<UID>
So for example:
https://facebook.com/profile.php?id=4
Will redirect you automatically to https://www.facebook.com/zuck Which is Mark Zuckerberg's profile.
If you want to do this for all your ids, then you can do it using a loop.
If you'd like, I can provide you with a snippet.
UPDATE: Alternatively, You can also do this:
https://facebook.com/<UID>
So that would be: https://facebook.com/4 which would automatically redirect to Zuck!
Accepted answer didn't work for me, this does:
https://www.facebook.com/app_scoped_user_id/10152384781676191
The easiest and the most correct (and legal) way is to use graph api.
Just perform the request: http://graph.facebook.com/4
which returns
{
"id": "4",
"name": "Mark Zuckerberg",
"first_name": "Mark",
"last_name": "Zuckerberg",
"link": "http://www.facebook.com/zuck",
"username": "zuck",
"gender": "male",
"locale": "en_US"
}
and take the link key.
You can also reduce the traffic by using fields parameter: http://graph.facebook.com/4?fields=link to get only what you need:
{
"link": "http://www.facebook.com/zuck",
"id": "4"
}
The marked answer seems outdated and it won't work.
Facebook now only gives unique ID related to app which isn't equal to userId and profileUrl and username will come out to be empty.
Doing me?fields=id,name,links is also depreciated after Graph Version 2.4
The only option now is to request for user_links permission from your developer console.
and the pass it in scope when doing facebook login
scope: ['user_link'] }
or by doing an api call
As of now (NOV-2019), graph.api V5.0
graph API says, refer graph api
A link to the person's Timeline. The link will only resolve if the person clicking the link is logged into Facebook and is a friend of the person whose profile is being viewed.
I've collected info together:
add into scope user_link, see https://developers.facebook.com/docs/facebook-login/permissions/
add into fields request link (e.g. https://graph.facebook.com/me?fields=link,name,email)
get link from answer. Be aware of field length- in my case it is 202: https://www.facebook.com/app_scoped_user_id/YXNpZADpBWEd0SlhFZAElYa3BQT3U3Tm4xWVRLSlJfYUdUM3Y4YmIwQjBaRkM0VDBMNURQdUhhYk5NRDJoR1ZA5ZA1JOdGNwampsSTQyMDQwbW93bkp0dnZAmOXg3NTFISFVZAQlRscWQ5eEZAvcU4xZAC1B/
And finaly: it doen't work without additional Facebook permission check:(

Retrieving facebook user wall posts through graph API

I'm currently working on a facebook app which captures wall posts for a specified user, page or group, after a user has authorized the app I quote the access_token that is returned to call the method
https://graph.facebook.com//feed?access_token=
this works fine for pages and groups as we only need a valid token, however for users the results are different depending on the relationship between the user who authorized the app the user whose posts are being captured.
For example if there is no relationship between the two users some posts are not returned even though they are public and displayed when you view the profile of the user, however if they are friends more posts are returned.
Can someone please explain this behaviour? And is it possible to obtain all posts using this method?
Yes, per the permissions listed at https://developers.facebook.com/docs/reference/api/permissions the results of the call are different depending upon the relationship.
However when things are public and you use an access token that doesn't belong to the user, then no results are returned. I cannot remember a time when this wasn't this way. For some odd reason (by design or omission on Facebook's part) using the graph API to get at public posts using a user access token just doesn't want to work.
For example, You can see some public items here
http://www.facebook.com/zuck
http://graph.facebook.com/zuck
However, you cannot seem to get any feed from here without an access token
https://graph.facebook.com/zuck/feed
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException"
}
}
Let's try adding an user access token that does not belong to zuck or a friend of zuck. https://graph.facebook.com/zuck/feed?access_token={UserAccessToken}
And here's what we get:
{
"data": [
]
}
What about an app access token? Let's try
https://graph.facebook.com/zuck/feed?access_token={AppAccessToken}
{
"error": {
"message": "Invalid OAuth access token signature.",
"type": "OAuthException"
}
}
So as you can see, it's difficult to get things via the graph that are not in alignment with your access token.
I manage to get most of the feeds from user's wall post. Although this is a old post, I hope someone can manage to get what they want from my answer.
Before getting the access token(to get the feeds), you need to add multiple correct Read Permissions "keys".
For example, you will have to add "read_stream", and "user_status" (which I found that these are the most important permission "key" to generate the correct access token, to retrieve one's so-called "public" feeds).
You can add more into generating the access token, in either you want the permission to GET, or POST, or BOTH.
Source is here: https://developers.facebook.com/docs/howtos/ios-6/#nativeauthdialog
One thing that I found is, the way to get the feeds result from user's "Home/About" page and Facebook Page (which is created for people to like (not add friends)) are different. The key is mentioned above, "read_stream", and "user_status". So it's better that you add both of the permissions in order to generate the access token to get everything in feeds.
Graph API doesn't return posts to the App if is not authorized to read user feed, even if posts are public.
Source: https://developers.facebook.com/bugs/290004301178437/
According to the latest api ,
FB.api("/me/feed","get",function(response) {//callback})
should do and also work well .
It is working now but facebook may change it in future.

Facebook graph API: ID of user profile picture

Is there a way to get the ID of the picture that is the current profile picture for a user?
Edit: OK, seems there is no trivial way to get the ID!
However, it appears from random testing that every user has a single album with attribute type="profile", and the first image in this album is always the profile picture, so it is possible to get the ID of the profile picture that way. Can some more experienced with Facebook confirm that this is indeed always the case?
You can get the users public profile photo from the following url:
https://graph.facebook.com/[id]/picture
Reference:
http://developers.facebook.com/docs/reference/api/
The answer seems to be yes
e.g., the photos in an album have an ID (the profile photo is a different object though, which a different FB ID). An ID for every object is a core concept of FB's new graph API: Every object in the social graph has a unique ID.
Indeed, data request i have ever made through the FB Graph API returns (when successful) a response in the form of a JSON array, comprised of nested ID-Value pairs, e.g. the education field from a User object:
"education": [
{
"school": {
"id": "126533127390327",
"name": "Massachusetts Institute of Technology"
},
"year": {
"id": "140829239279495",
"name": "1992"
},
"concentration": [
{
"id": "192578844099494",
"name": "Computer Science"
}
],
"type": "College"
}
The profile photo is a connection of the User object (i.e., every FB object has Fields and Connections).
According to the table in the relevant FB Developer Page, a call to the Graph API requesting picture (user's profile photo(s)) returns a string which is the URL for the user's profile picture.
But why doesn't this same call return the user's profile photo ID?
The reason i suppose is that the URL returns:
Graph API : User Properties
The user's profile photo is not there, but in an adjacent node:
Graph API : User : Connections : picture
(See FB Documentation for Graph API structure here).
When you make that API call, examine the Request Headers, and in particular, the Request URL; when you do, you'll see something like this:
http://profile.ak.fbcdn.net/hprofile-ak-snc4/32093_100005743929541_5467982_q.jpg
The string between the underscores (100005743929541) is the user ID. this is easy to verify by making another call to the Graph API. From your browser location bar (assuming you are logged into Facebook) just enter:
https://graph.facebook.com/me
So again, the first item in that JSON string is the user's FB ID. Given that, it seems to me that if user profile ID does indeed have its own ID, then that string (user's FB ID) plus the two smaller adjacent strings of integers on either end of the ID, should be it--in other words, 32093_100005743929541_5467982 in the Request URL above.
Finally, perhaps the best way to answer this is by using the new Graph API Explorer. (I just tried to verify this, but my requests are hanging.)
There is one more simple way to accomplish this task.
Fetch all facebook albums by https://graph.facebook.com/me/albums?access_token=.
Loop into the albums and check if object type == 'profile'.
Get the value of object cover_photo where type == 'profile'.
This value of cover_photo is the id of profile pic of the user.
You can check it by https://graph.facebook.com/<cover_photo value>?access_tokne=
I don't know if you still need this but this is the way I did it in PHP:
$albumsfb = $facebook->api('/me/albums/?fields=type');
$albumsfb = $albumsfb['data'];
foreach ($albumsfb as $albumfb) {
if ($albumfb['type']=='profile'){
$albumprofile = $albumfb['id'];
break;
};
}
$photoprofilefb = $facebook->api('/'.$albumprofile.'/?fields=cover_photo');
$photoprofilefb = $photoprofilefb['cover_photo'];
$photoprofilefb = $facebook->api('/'.$photoprofilefb);
$fotoperfilfburl = $photoprofilefb['source'];
Instead of "me", put the user id that you want?
http://graph.facebook.com/<SOCIAL-ID>/picture?type=<small|normal|album|large|square>
SOCIAL-ID = Facebook returned social id.
small|normal|album|large|square = size of image.
I recently tried to like one of my friend's profile pictures from the Graph API explorer
Here's how I got the id of his current profile pic:
fields=friends..fields(albums.fields(photos.fields(id,name,link),name))
If you have used the Graph API explorer, you might be aware of the above line of code that's generated in the textEntry when you tick the checkboxes. The limit size is up to you, I just kept it 1 to receive the JSON object quickly (without the limit, it took me 5 minutes to get the JSON object containing a lot of information).
So here are the things you need to check in the Graph API (maintain the order):
Friends
Within Friends Albums
Within Albums Name and Photos (Within Photos the id and name of the photo)
I made a PHP script to like all the profile pictures of my friends :)