How to determine if a Facebook user has uploaded a profile picture or its default? - facebook

Is there a way to know if a user uploaded an image to the profile or it has the default user picture of Facebook via FQL or somthing else?

If a user doesn't have a photo then the is_silhouette field will be true when you request the user object with the "photo" field specified.
Example request:
https://graph.facebook.com/username?fields=picture
Response:
{
"id": "100002095576350",
"picture": {
"data": {
"url": "http://profile.ak.fbcdn.net/static-ak/rsrc.php/v2/yo/r/UlIqmHJn-SK.gif",
"is_silhouette": true
}
}
}
Quick, dirty PHP function:
function facebook_user_has_photo($username_or_id){
$request = file_get_contents('https://graph.facebook.com/'.$username_or_id.'?fields=picture');
if($request):
$user = json_decode($request);
if($user && !$user->picture->data->is_silhouette) return true;
endif;
return false;
}

You can use the Python script below (as not mentioned any programming language) to make it work.
urllib.urlopen('https://graph.facebook.com/<PROFILE_ID>/picture?access_token=%s' % access_token).geturl()
This will provide you a Facebook profile photo URL. If that URL contains <PROFILE_ID> then it uploads an image. Else the default Facebook image is uploaded.
For example, if uploaded image:
http://profile.ak.fbcdn.net/hprofile-ak-snc4/195361_<PROFILE_ID>_4179703_q.jpg
else:
For male:
http://b.static.ak.fbcdn.net/rsrc.php/v1/yo/r/UlIqmHJn-SK.gif
For female:
https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v1/y9/r/IB7NOFmPw2a.gif
I hope this helps.

From my experiments with the Facebook API, it appears one has to actually get the picture in order to tell whether it's a static default one or not.
At the time of writing, it appears that all uploaded photos on Facebook are converted to JPEG, while the static default pictures are in the GIF format. (BTW, this is not consistent with some thumbnail sizes).
Looking for specific GIF file or a specific URL path is not reliable (note that there are CDN URLs involved, and that there are different static files for male and female). Assuming Facebook doesn't re-encode their entire profiles photos database, I suppose looking for a GIF is reliable enough.
Here's a sample PHP function to do this. I've tested it successfully with my 120 Facebook friends, and it seems to do the job.
public static function hasProfilePicture($fbuid)
{
/* Really stupid method to test if Facebook user has real profile picture
* based on Facebook returning a GIF image when you request a large photo.
* Use with care - for every profile there's an outgoing request! */
$r = get_headers("http://graph.facebook.com/$fbuid/picture?type=square");
return !array_search("Content-Type: image/gif",$r);
}

Related

Get photo from post using Facebook graph API

I am attempting to display the latest post from a public Facebook page on a website using the graph API.
Using the following URL, I am able to get the latest posts data:
https://graph.facebook.com/MYPAGENAME/posts?access_token=MYACCESSTOKEN
Which returns:
{
"data": [
{
"story": "MYPAGENAME added a new photo.",
"created_time": "2017-08-20T19:00:00+0000",
"id": "MYID"
}
...
The post in question displays on the Facebook page with a photo included, but there is not 'photo' object (or anything similar) returned in the API data. How do I access the picture URL for this post? Thanks in advance.
This request will fetch posts and the media information for the first image for each post.
/posts?fields=attachments{media}?access_token=YOUR_ACCESS_TOKEN
Sometimes posts contain multiple images. If you'd like to support that you need to fetch the subattachments also:
/posts?fields=attachments{subattachments{media}}?access_token=YOUR_ACCESS_TOKEN
Note that this method usually only provides image URLs that are up to 720px wide. If you want higher resolution imagery, you need to access data[0].attachments.data[0].subattachments.data[0].target.id to get the actual image ID which you can then use to perform an additional query /IMAGE_ID_HERE?fields=images to obtain the higher resolution image. Increment the numbers to get additional posts and images inside each post.

Can you retrieve facebook photos in Unity?

Is there anyway to retrieve photos that are not the profile picture on Unity through the facebook api? or any other way?
1. Easiest
Use www class to fetch the image texture.
string link = https://graph.facebook.com/v2.1/" + friendID + "/picture?width=50&height=50&redirect=true;
WWW pic = new WWW (link);
yield return pic;
if (pic.error == null)
{
go.renderer.material.mainTexture = pic.texture;
}
where friendID is your facebook id. Here you can pass the id for any of your friends.
width and height to specify what dimension do you want the image to be.
2. A bit complex but useful in long run
link to pass to www class
string link = https://graph.facebook.com/v2.1/" + friendID + "/picture?width=50&height=50&redirect=false;
the difference in this method is that you are now not redirecting to the cdn permanent link.
this will give you a json file. Now you can parse the json file to get the actual link.
use this link again with www class to get the actual image
benefits of using this method.
You can save the image once and next time just ping for permanent link only.. if they match then don't fetch image again.
You're in luck. Facebook themselves have made a tutorial for Unity, which is a small game using Facebook's profile pictures. Here is a link to it.
Yes you can. Not sure what platform are you using.
You can use Facebook official SDK from here
https://developers.facebook.com/docs/unity
Authorise your platform, facebook will give you a token.
Then you can call graph API to get whatever you need.
For Example:
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Did%2Cname%2Cphotos&version=v2.2&

Through APIs: How to get publicly sharable link of a Facebook Album that is locked or set to private?

Is there a way to get the public link of a Facebook Album through the Facebook API (graph/fql) that can be shared with those who are not on Facebook?
This Album is not publicly accessible. It is restricted by the share permissions on the album set by the user. But I am looking for a way to generate the public link of this restricted album so whoever is sent this link can view the album unconditionally.
I know this is possible through the Share Album feature on the Facebook website through the Share Dialog: You can send this album to friends or relatives by giving them a link.
I couldn't find a way to to this in the Graph API.
EDIT: I'm talking about the public link that is available through this feature on facebook: facebook.com/help/124590517619792
You didn't provide an example or test case, nor any API code to work with. I'll use the following links for Facebook User Name Hostess as an example (long live Twinkies!).:
Main: https://www.facebook.com/Hostess
Photo Album: https://www.facebook.com/Hostess/photos_albums
First, we will need the Facebook User Number.
You can perform a GET request or type this into the browser:
Facebook User Info: http://graph.facebook.com/Hostess
To continue with this example, we will use the Facebook Number provided by the previous step:
Facebook ID: 145369525544570
Is there a way to get the public link of a Facebook Album through the
Facebook API (graph/fql) that can be shared with those who are not on
Facebook?
Yes there's a way! This link will provide you with json results for all albums, in this case 6:
Facebook Albums via json: http://graph.facebook.com/145369525544570/albums
To limit the returned results, you can use ?limit=1 (where 1 is the amount of albums returned):
Facebook Album (1) via json: http://graph.facebook.com/145369525544570/albums?limit=1
This Album is not publicly accessible. It is restricted by the share
permissions on the album set by the user. But I am looking for a way
to generate the public link of this restricted album so whoever is
sent this link can view the album unconditionally.
Your out of luck and there isn't a way. However, you don't have to be a Facebook Member to view the images in the Album. Using the json data from the previous step works when the Album is made public. Just use a simple GET request:
jsFiddle DEMO: Facebook Albums Links from Facebook ID
// Console log messages are provided, activate your browsers console.
$.ajax({
url: 'http://graph.facebook.com/145369525544570/albums',
dataType: "json",
success: function(yourVar) {
// Make sure the request, 1 json object, was returned.
if (yourVar) {
console.log('This is the jQuery Object received. Click on it to explore contents');
console.log(yourVar);
// This will parse the Objects received in 'data' variable.
// Having said that, it's coincidental the data the wrapped in a 'data' name.
$(yourVar.data).each(function(index) {
// Only process Objects that have valid Album links.
// Having said that, only Photo Albums are returned in initial data, not Video Albums.
if(this.link){
// Display in console the jQuery zero indexed number
console.info('Album Index object: ' + index);
//
// Display in HTML via append, the same information.
$('body').append('<b>Album Index Object: </b>' + index + '<br />');
// Display in console the Album Names available
console.info('Name: ' + this.name);
//
// Display in HTML via append, the same information.
$('body').append('<b>Name: </b>' + this.name + '<br />');
// Display in console the Album Name Url;
console.log('Url: ' + this.link);
//
// Display in HTML via append, the same information.
$('body').append('<b>Url: </b>' + this.link + '<br />');
// Display in console dashed lines before next item is shown.
console.log('-------');
//
// Display in HTML via append, the same information.
$('body').append('<hr><br /><br />');
}
});
} else {
console.log('Was data expected? Check your jQuery or JavaScript for errors.');
}
}
});
// Power-Tip!
// You can also use .ajax() with a YQL Rest Query url.
// See YQL Console with Rest Statement at:
// http://developer.yahoo.com/yql/console/?q=SELECT%20data.link%20FROM%20json%20WHERE%20url%3D%22https%3A%2F%2Fgraph.facebook.com%2F145369525544570%2Falbums%22&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
// As seen there, the data.link is providing just the links.
// Change data.link to * to see all data nodes available in json tree.
// That being said, your .ajax() url for above is at the bottom: The Rest Query.
In Detail: Why it can't be done. (otherwise it's a security issue!!!)
Facebook Settings for Photos and Albums:
When I share something, how do I choose who can see it?
The above Facebook documentation states that if a Album is set to Private via Only Me, then it will only be available to you, even though you may have a shareable link for it.
To demonstrate that the Album/Photos is indeed locked and not shareable, here is an actual live test using Yahoo! Query Language to perform a request on a locked photo album:
Feel free to click the image to visit the above YQL Statement.
To be sure, the above example in itself shows that the link is not the issue, it's the content that is really the subject, as that's the item locked for which a Facebook Token key is required.

facebook oauth, no picture with basic permissions

I'm using oauth to log facebook users in to my app. I'm testing with my FB account and with the basic permissions scope. Facebook docs say that I should be able to get 'picture' with basic permissions, but my account has no 'picture' property when I access it with the API. First name, last name, etc. are there though.
Is this because my account is not publicly viewable? Why might this happen? I definitely have a profile picture attached.
Here's my fb link: http://www.facebook.com/josh.nankin
best way is to keep the basic permissions and fire the request for the image in a separate step. Ask the graph api in this way:
https://graph.facebook.com/[fb_user_id]?fields=picture.type(small)
larger image:
https://graph.facebook.com/[fb_user_id]?fields=picture.type(large)
You will get a JSON response like:
{
"id": "100001XXXXXXXXX",
"picture": {
"data": {
"url": "http://profile.ak.fbcdn.net/hprofile-XXXX/41524_1000018XXX51507_XXXX3_q.jpg",
"is_silhouette": false
}
}
}
You can derive the profile photo URL directly from the basic standard OAuth info returned from facebook as Josh Nankin noted, BUT with a clarification.
You can still use the user's ID returned to request the photo, for example:
https://graph.facebook.com/914431778578699/picture?type=large
is the same as
https://graph.facebook.com/dark.eye.1/picture?type=large
This way only one request must be made, because you can interpolate the photo URL as you store it in the DB.
So, still not sure why the graph api does not return a picture property when you hit the /me path on the API, but once you have the username from /me, you can get the picture pretty easily at:
https://graph.facebook.com/USERNAME/picture
https://developers.facebook.com/docs/reference/api/user/:
“picture: The URL of the user's profile pic (only returned if you explicitly specify a 'fields=picture' param)”
You haven’t probably overlooked the part marked in bold …?
Actually you don't need to allow public access to get the profile picture since you can get the USER_ID. Though the USER_ID you can navigate to the profile picture easily.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
console.log('Welcome! Fetching your information.... ');
var url = '/me?fields=id,name';
FB.api(url, function(response) {
var linkpp = 'https://graph.facebook.com/' + response.id + '/picture?type=large';
document.getElementById("id_of_link_tag").href = linkpp;
});
});
}
You can change the type in variable linkpp to type=small which returns a low resolution image.
var linkpp = 'https://graph.facebook.com/' + response.id + '/picture?type=small';

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 :)