facebook oauth, no picture with basic permissions - facebook

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';

Related

Different Facebook access token JavaScript SDK

I'm quite confused with the access token from facebook..here is how I obtain the user's access token and use it to get data from graph API
window.fbAsyncInit = function() {
FB.init({
appId : 'app ID',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
FB.getLoginStatus(getStatus);
FB.Event.subscribe('auth.authResponseChange', getStatus);
function getStatus(response) {
if ( response.status === 'connected' ) {
var accessToken = response.authResponse.accessToken;
console.log("accessToken = " + accessToken);
$.ajax({
dataType : "jsonp",
type : "GET",
url : "https://graph.facebook.com/me/albums?access_token=" + accessToken,
success : function(data) {
$.each(data, function(index, value) {
console.log(index + ": " + value);
})
}
});
}
}
};
However, I always get an empty data. After I visit the graph API documentation here: https://developers.facebook.com/docs/reference/api/ and click on one of the graph links, I notice that the access token generated there is always different from what I retrieve from my code. For example, the current access token in the graph api documentation is
"AAAAAAITEghMBAMwuyHZCO3VOAvCm9hHpaZC9PGV9238ixsZB7zSfuplZBTZCLRj6cEViZADJlVcjOfInwvcbhqu3XBF1w4ZAxvPbexcGQZAYzb4bHAKsMbLF"
and the one in console log is
"AAAG0ZCFantJ8BAAFcMdDOyDyT4OBtjrvULEaS2o94gZAU7U1xITaogFXCZBghQP8G9bjEh3XSCATQOZCUSZCuNWFvEfypIAmcz9bkbk5qRBlHUZAOE4guW"
I think that I may have done this the wrong way. Can anyone help explain to me how to retrieve data from graph API in a correct way?
Any kinds of help will be appreciated. Thanks in advance :)
Run this URL with your authToken first:
https://graph.facebook.com/me/permissions?access_token=USER_ACCESS_TOKEN
You'll almost certainly only see basic permissions in the response, which is why you aren't getting the data from your call to the user's album.
You'll then want to run your user through the Authentication Process, making sure you request the permissions you need (probably 'user_photos' in your case).
More info is available in those two links. Good luck!
access token will never be the same. So that behaviour is correct.
Back to your problem , I think its mostly scope related issue.
I had developed a application using facebook c# sdk, In that application i did it like this:
when a post is to be submitted , redirect the user to facebook (with some parameters like appid, app-secret, auth-token and
redirect-url);
Facebook will ask the user to login with his credentials.
When the user is logged in, facebook will redirect back to the redirect-url. (with the authtoken and a new auth-code).
Then we should use this auth-code, app-id and app-secret and obtain the user-access-token. (This is done by doing a rest api call to the
url
https://graph.facebook.com/oauth/access_token?client_id=client_id&redirect_uri=redirect_uri&client_secret=client_secret&code=auth-code
This will return the user-token and expiry time for the token
And then we can post using this user-access-token to the fb.
Initially i had misunderstood the auth-code and used it as the user-access-token, and i always got the result as unauthorised token .
Please check whether this will help you or not.

How to get all facebook pages id associated with my account?

I want to get the statistics of all the pages associated with my account on facebook, i have the code to get the statistics of a page, i just want to get the page id of all the pages associated with my account, how can i do this?
Please help.
Thanks.
You'll need to request the manage_pages permission (more details here), once you have the aquired the permission, all you have to do is make a request to :
https://graph.facebook.com/YOUR_FACEBOOK_ID/accounts.
The return value will be a JSON with all the pages/applications that are associated with your account.
You can also use this great tool that facebook provides - The Graph API Explorer (/me/accounts) to explore what other information you can retrieve without having to write a single line of code :P very useful.
FB.api('/me/accounts', function(response) {
for (id in response.data)
{
page = data[id];
page.id;
page.name;
}
});
You can use Javascript API to do this, read more :http://developers.facebook.com/docs/reference/javascript/

Facebook tagging user on a photo in a Page Gallery via SDK and Graph is possible?

I'm trying to tag a user (having its userid), with a Graph request made to a picture just uploaded to a Page Gallery.
Permissions I use in the app are:
#"read_stream", #"offline_access", #"publish_stream", #"manage_pages",#"photo_upload",#"user_photos" and with a access_token to the session got from page_it?fields=access_token item (that works for picture upload in gallery).
And I use them to upload the photo to a Gallery on a Fan Page successfully.
When I try to tag the user with a graph request in the form:
POST to picture_id/tags with param: "to"->userid
I get only facebookErrDomain error 10000 as if permissions are not right.
From API Documentation seems that only user_photos and publish_stream are required and no mention to page-galleries photo is made.
I'm quite clueless about this issue.
It's possible. You need to collect the page's specific token by placing a call to /me/accounts. And then tag the photo using page's access token. Once you get that, you can tag the photo using it's Graph Object Id and user id whom you are tagging.
if (accessToken) {
var data = {
"access_token": accessToken,
to:userId,
x:x,
y:y
}
FB.api("/" + photoId+"/tags/"+userId , 'post', data, function(response) {
//here is the response
});
}
Hope it helps. The key is the access token that is specific to that page :)

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

How to get facebook profile picture with 'F'-logo?

I use Graph API and I need to get users' profiles pictures by their UIDs.
It's easy to do: http://graph.facebook.com/[Facebook UID]/picture
I remember, some time ago, there was a litte Facebook Logo (litter 'f'-char) on these profile pictures. But now, I get only profile pictures without this logo.
How can I get images with this 'f'-logo?
Facebook doesn't store images with the 'f' on, so it's a little hacky to get at them.
I've found that this works:
You'll need the final image url, after graph.facebook.com/[id]/picture redirects.
http://external.ak.fbcdn.net/safe_image.php?&url=[imageurl]&logo
eg. me
I think it's not possible using the graph api.
It was possible using FQL (dont know if it is still working):
SELECT pic_with_logo FROM user WHERE uid = "..."
or use xfbml with <fb:profile-pic facebook-logo="true">
With jQuery and JS API you can make an FQL call:
FB.api({ method: 'fql.query', query: 'SELECT uid, pic_with_logo FROM user WHERE uid = me()' }, function(response) {
var user = response[0];
$('#container').append('<img src="'+user.pic_with_logo+'"/><br>');
});
also you can try:
pic_small_with_logo, pic_big_with_logo, pic_square_with_logo and the pic_with_logo i use in example.
here is the user table:
https://developers.facebook.com/docs/reference/fql/user/
Based on Tom's answer, Here is php example to get user's
$_json = json_decode(file_get_contents('http://graph.facebook.com/[FACEBOOK_UID]?fields=picture'));
if (isset($_json->picture)) {
header("Location: http://external.ak.fbcdn.net/safe_image.php?&url=" . $_json->picture . "&logo");
exit;
}
Here is what we do:
Query picture url via opengraph
Get the picture url from json object
redirect the page to facebook image handler with addition &logo to url