Facebook Graph API: Uploading Photo to specific album - facebook

I'm trying to upload photos to a specific album of the authenticated Facebook user. But the photo does not appear in the album. It appears in a new album named '"appname" Photos'. I'll try to describe my way as complete as possible - maybe this helps you out there to find my mistake.
I'm not using any Facebook or third party SDK. The project programming language is C# (if it matters).
First of course, I'm getting an access token, by sending the user to the following URL:
https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&response_type={2}&scope={3}
{0}: The client ID: my application ID, as provided by Facebook
{1}: The redirect URI, set to 'https://www.facebook.com/connect/login_success.html', as I'm working on a desktop application
{2}: The response type: 'token'
{3}: The scope - all the privileges I'm requesting. This is a comma separated list of the following items:
manage_pages
email
user_about_me
user_photos
user_videos
publish_actions
publish_stream
user_likes
user_activities
user_groups
photo_upload
video_upload
The list of privileges is not only meant for the photo upload.
The login and it's implementation works quite well, so that I get an 'access_token' and an 'expires_in' field in the answer.
If I don't have it already saved, I'm getting the user ID of the authenticated user represented by the access token. This is done by a simple request to:
https://graph.facebook.com/me?access_token={0}
{0} is the acquired access token
This also works quite well and I get a positive response with name, e-mail etc. and the user ID.
Next Task, get the list of albums the user associated with the acquired access token and the user ID. This is done by a FQL query:
SELECT aid,can_upload,created,name,description FROM album WHERE owner={0}
{0} is the user ID
The FQL query is executed by calling this URL:
https://graph.facebook.com/fql?q={0}&access_token={1}
{0} the query from above
{1} the access token
Again, this works quite fine - I get the requested fields back. The 'aid' values are used as album IDs later and are in the format 99999999_999999999 (numbers with an underscore somewhere in the middle). I only use albums that have the can_upload flag on "true".
Now, I hope I have all the information I need to publish the photos.
I create a POST request, multipart/form-data, to this URL:
https://graph.facebook.com/{0}/photos?access_token={1}
{0} is the 9999999_999999 album ID
{1} is the access token from the login
The request consists of two fields: 'source' contains the photo's binary data, 'message' the caption.
The request succeeds. No problem detectable for me.
But when I now visit the Facebook user page, the photos are not in the selected album, they are in an extra album named '"appname" Photos'. The caption is set correctly.
What must I change to bring the photos directly to the desired albums?

The aid is not the ID you should be using when you do the upload. This is not obvious from the Facebook docs - but you should use the object_id column as this represents an object on the Graph API.
So, change your FQL query to:
SELECT object_id,can_upload,created,name,description FROM album WHERE owner={0}
The object_id is just a number - not two numbers separated with an underscore. So make your post to:
https://graph.facebook.com/{0}/photos?access_token={1}
{0} is the 9999999 object ID
{1} is the access token from the login
Note that you only need user_photos permission to upload photos. But without publish_stream permission they do not appear in the user's album straight away - instead, when the user browses to the album they will see this:
Once they click approve they will appear in the album. If your application has publish_stream then they will go straight into the album and the user will not need to approve them.
The rest of your code should be correct.

The redirect URI, set to
'https://www.facebook.com/connect/login_success.html', as I'm working
on a desktop application
I'm not clear how you are getting USER'S access token in this case. To me it looks like you don't get it but instead you are using your APP'S token - that's why weird album name. Can you double check in Token Debugger that you indeed use user's token?
I have similar workflow for one of my windows forms app - but I do redirect user to localhost - that's where my WCF service is listening and acquiring user's access token from.

Related

creating album and posting a photo in facebook page

I'm trying to create an album and post photo to it , these operations have to be done on a "facebook page". Before writing any code i tested it on graph api explorer :
i tries following to create an album
/page_id/albums method:post
fields :
name : "test"
access_token : "valid access_token"
i got the above mentioned access token after granting permissions like :manage_pages , photo_upload . Any ideas where i might be going wrong.
I have gone through the documents and stackoverflow questions but can't seem to figure out the problem.
i resolved the error , we have to set the access token to access token of the page . the access token can be obtained by issuing :
/me/accounts using get method.
also important thing is this will only show the list of pages to which the current user is admin to.
the access token received from the graph explorer is valid for only 1 hour, but if you receive the access token by issuing $facebook->api('/me/accounts') in php then that access token never expires.
to check if the access token is valid and related info the following link can be used to check:

Get user profile picture by Id

I'm now working on a web application which is mostly based of facebook graph api.
I hold some data about users - actually , the possible public data available - such as name and id.
I also know that a profile picture is a part of the public data, and I wondered how I'll be able to get a direct link to a profile picture of a user only by using their id?
http://graph.facebook.com/" + facebookId + "/picture?type=square
For instance:
http://graph.facebook.com/67563683055/picture?type=square
There are also more sizes besides "square". See the docs.
Update September 2020
As Facebook have updated their docs this method is not working anymore without a token. You need to append some kind of access_token. You can find further information and how to do it correctly in the fb docs to the graph api of user picture
Requirements Change
This endpoint supports App-Scoped User IDs (ASID), User IDs (UID), and Page-Scoped User IDs (PSID). Currently you can query ASIDs and UIDs with no requirements. However, beginning October 24, 2020, an access token will be required for all UID-based queries. If you query a UID and thus must include a token:
use a User access token for Facebook Login authenticated requests
use a Page access token for page-scoped requests
use an App access token for server-side requests
use a Client access token for mobile or web client-side requests
Quote of fb docs
You can use following urls to obtain different sizes of profile images.
Please make sure to add Facebook id to url.
Large size photo
https://graph.facebook.com/{facebookId}/picture?type=large
Medium size photo
https://graph.facebook.com/{facebookId}/picture?type=normal
Small size photo
https://graph.facebook.com/{facebookId}/picture?type=small
Square photo
https://graph.facebook.com/{facebookId}/picture?type=square
To get largest size of the image
https://graph.facebook.com/{userID}?fields=picture.width(720).height(720)
or anything else you need as size. Based on experience, type=large is not the largest result you can obtain.
From the Graph API documentation.
/OBJECT_ID/picture returns a redirect to the object's picture (in this case the users)
/OBJECT_ID/?fields=picture returns the picture's URL
Examples:
<img src="https://graph.facebook.com/4/picture"/> uses a HTTP 301 redirect to Zuck's profile picture
https://graph.facebook.com/4?fields=picture returns the URL itself
This will be helpful link:
http://graph.facebook.com/893914824028397/picture?type=large&redirect=true&width=500&height=500
You can set height and width as you needed
893914824028397 is facebookid
UPDATE September 2020
Facebook has new requirements change: an access token will be required for all UID-based queries
So you have to add your app access token to the url:
https://graph.facebook.com/{profile_id}/picture?type=large&access_token={app_access_token}
To get your app_access_token use the following url:
https://graph.facebook.com/oauth/access_token?client_id={your-app-id}&client_secret={your-app-secret}&grant_type=client_credentials
You find your-app-id and your-app-secret in the Basic Settings of your Facebook app in Facebook developers
https://www.avatars.io/
It's an API that returns the profile image when given a username for a variety of social networks including Twitter, Facebook, Instagram, and gravatar. It has libraries for iOS, Android, Ruby, Node, PHP, Python, and JavaScript.
you can get it with using this url : you will get the picture HD (max size)
https://graph.facebook.com/{userID}?fields=picture.width(720).height(720)&redirect=false
don't forget redirect=false or it will return an error
Use url as:https://graph.facebook.com/user_id/picture?type=square in src of img tag.
type may be small,large.
You can use AngularJs for this, Its two -way data binding feature will get solution with minimum effort and less code.
<div>
<input type="text" name="" ng-model="fbid"><br/>
<img src="https://graph.facebook.com/{{fbid}}/picture?type=normal">
</div>
I hope this answers your query.Note: You can use other library as well.
Through the Javascript SDK (v2.12 - April, 2017) you can get the details of the picture request this way:
FB.api("/" + uid + "/picture?redirect=0", function (response) {
console.log(response);
// prints the following:
//data: {
// height: 50
// is_silhouette: false
// url: "https://lookaside.facebook.com/platform/profilepic/?asid=…&height=50&width=50&ext=…&hash…"
// width: 50
//}
if (response && !response.error) {
// change the src attribute of img elements
[...document.getElementsByClassName('fb-user-img')].forEach(
i => i.src = response.data.url
);
// OR redirect to the URL above
location.assign(response.data.url);
}
});
For getting the JSON response the parameter redirect with 0 (zero) as value is important since the request redirects to the image by default. You may still add other parameters in the same URL. Examples:
"/" + uid + "/picture?redirect=0&width=100&height=100": a 100x100 image will be returned;
"/" + uid + "/picture?redirect=0&type=large": a 200x200 image is returned. Other possible type values include: small, normal, album, and square.
As per the current latest Facebook API version 3.2, For users you can use this generic method for getting profile picture is https://graph.facebook.com/v3.2/{user-id}/picture?type=square you can visit documentation for user picture here.
The possible values for type parameter in URL can be small, normal, album, large, square
For Groups and Pages, the profile picture is not available directly. You have to get them using access token. For Groups you have to use User Access Token and for Pages you can use both User Access Token and Page Access Token.
You can get Group's or Page's Profile Picture using the generic URL: https://graph.facebook.com/v3.2/{user-id}/picture?access_token={access-token}&type=square
I hope this is helpful for people who are looking for Page or Group Profile picture.
Simply Follow as below URL
https://graph.facebook.com/facebook_user_id/picture?type=square
type may be normal,small,medium,large. Or square (f you want to get square picture, the square size is limited to 50x50).
today I found a problem, the profile images was returning the default profile picture because a new requirement from Facebook to all the UID based queries, so just add &access_token=[apptoken] to the url, you can obtain your app token from here
You can use the following endpoint to get the image.jfif instead of jpg:
https://graph.facebook.com/v3.2/{user-id}/picture
Note that you won't be able to see the image, only download it.
Update July 2021
On top of answer by #EddyG, turned out that if you still not able to fetch the profile picture with app access token, the FB account must "allow" the FB app first then you should be able to fetch the profile picture with FB app access token.

In terms of privacy, is it acceptable to extract the user id from a Facebook share response to make use of that user's details?

Using the Facebook API, you can get back the post ID of a Facebook share, even if the user has not authorized your app.
The post ID is prefixed by your user ID, and separated by an underscore.
FB.ui({
method: 'feed',
name: 'People Argue Just to Win',
link: 'http://www.nytimes.com/2011/...',
}, function( response ){
// Response gives you :
// { post_id: "[fbUserId]_346759642059572" }
});
To me it looks like Facebook is using this programmatically, rather than with the idea of providing us the userId out of the kindness of their hearts. But it's extremely tempting to use.
I'm a little rusty on permissions - if there is a way to get back all the users that have liked/shared a specific URL, and not just a count, then this should be okay.
But the question remains, is it acceptable to use?
EDIT:
Now that I think about it, you can access the user ID by making an anonymous call to https://graph.facebook.com/[postId] but ONLY if the post was made public.
If you get a response from FB, it means that you have already ask the user for the required permissions,
so yes you can use the data returned from Facebook as you like, but you always have to inform the users how you use those data.
You can only cache user data if you have permission or it is necessary for the function of your app. Using the like button you can subscribe to edge.create and cache the response from the like.
refer to: http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/
II. Storing and Using Data You Receive From Us
You will only request the data you need to operate your application.
You may cache data you receive through use of the Facebook API in
order to improve your application’s user experience, but you should
try to keep the data up to date. This permission does not give you any
rights to such data.
in my app i store the id of every page or user that is viewed to speed up page load times and count page views. Seems with in the scope of Policy as long as i am not sharing the info with 3rd parties or making public what a user has set to non public.

Can we get a facebook access token by providing email and password in the code?

I would like to get the photo urls from the from this album:
https://graph.facebook.com/175758929145317/photos
If you access it without providing an access token, then you will get empty data:
{
"data": [
]
}
But if you first generate an access token here:
http://developers.facebook.com/tools/explorer?method=GET&path=175758929145317%2Fphotos
And then you append it to the url like this, you can get the data:
https://graph.facebook.com/175758929145317/photos?access_token=your_access_token
However, this access token cannot be used forever, it will expire.
I would like to dynamically take the photo urls from https://graph.facebook.com/175758929145317/photos and display the images on a webpage, the person who views this webpage does not need to have a facebook account.
So every time the webpage is viewed, I am thinking about if it's possible to simply use my facebook account to login (by hardcoding the email and password) and then obtain a new access token so that I can get the photo urls.
Is this possible? Or is there a better solution to my problem? (I cannot ask the album owner to change his album to public)
it's not possible, you cannot authenticate in FB using hardcoded (or stored) login/password - as per Facebook policy:
I. Features and Functionality
...
3 - You must not include functionality that proxies, requests or
collects Facebook usernames or passwords.
what you can do instead:
Get (and then extend) token for yourself (see scenario 4 in this document: https://developers.facebook.com/roadmap/offline-access-removal/). That extended token should be good for 60 days - and then you might be able to extend it again.
Try to get those photos using APPLICATION token - and if it works, then the token is good forever: https://developers.facebook.com/docs/authentication/applications/

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.