Get photo from post using Facebook graph API - facebook

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.

Related

How to get Instagram post URL from Facebook Graph media_id

I'm using Instagram Mentions API and was able to obtain FB media_id for couple Instagram posts.
I need media_id to get post content via /user/mentioned_media. Unfortunately that endpoint does not provide ig_id or shortcode or permalink which I need.
Is there a way to get Instagram Post URL with FB Graph API media_id?
It seems like you can get media metadata (and all comments/replies) when someone #-mentions you in a non-owned-media comment by using a query like below.
You would use your own instagram business account id for the first number (the path component), and you'd use the comment_id you received from the webhook comment-mention notification as the second number:
/12345678901234567890?fields=mentioned_comment.comment_id(9876543210987654321){id,text,username,timestamp,media{id,media_type,media_url,permalink,username,like_count,comments_count,comments{id,text,username,user{id,username},like_count,timestamp,replies{id,text,username,user{id,username},like_count,timestamp}}}}
Yes, there is a way you can get Instagram post URL associated with a media if you know the media id.
You can also get more information (all its fields and edges) on media. See the docs here
You can get the Instagram post URL by making a GET request to /{ig-media-id}
Sample Request:
GET https://graph.facebook.com/v15.0/{{media_id}}?fields=permalink, shortcode&access_token={{ACCESS_TOKEN}}
Sample Response:
{
"permalink": "https://www.instagram.com/p/Clxsi5Pol2_/",
"shortcode": "Clxsi5Pol2_",
"id": "17xxx59557977xxx"
}
The value of the permalink is the valid public URL for the post.
You can also use the shortcode returned to construct the URL by concatenating the shortcode to https://www.instagram.com/p/.

How to obtain the live video id of a facebook live video?

I am trying to access the live views count of a live video that is live through a Facebook Page. I am using the ID that I am obtaining from the API request {page-id}/videos/uploaded When I am using that video id and making a GET request of {video-id}?fields='live_views its showing error code of 100 with the message as:
(#100) Tried accessing nonexisting field (live_views) on node type (Video)
I have the following permissions:
'manage_pages', 'publish_pages', 'business_management', 'read_insights', 'user_videos'
I am making the GET request using user_access_token.
I am using Graph API Version 2.8.
Please let me know if I am using the wrong ID. IF yes, then how can i get the live_video_id of a live video posted in page?
To get the live views count of a video a fan page, you need:
An app created at facebook developer
A user access token (for testing, e.g. by calling https://developers.facebook.com/tools/accesstoken/)
The fan page id
With this you can call graph API in the following format:
https://graph.facebook.com/v2.8/<fan_page_id>/live_videos?access_token=<access_token>?fields=live_views
Of course, you may add more elements in fields parameter if you need more information. Here Facebook's Graph API explorer helps: https://developers.facebook.com/tools/explorer
When you add status to fields parameter, "LIVE" means, it's currently live, "VOD" means it has already finished.
If you try to implement a WebHook for it, let me now if it works. Thanks.
To get live views you need to call the api with the live video id, which you get from calling, page id can initially be either the page-id or name
"https://graph.facebook.com/v10.0/${page_id}/live_videos?access_token=${access_token}
Sample response:
{
"title": "hello world 2",
"status": "LIVE",
"embed_html": "...,
"id": "123123" // this is the live video id
},
After you have retrieved the live video id you can call this endpoint to get the live_views edge
https://graph.facebook.com/v10.0/${live_id}fields=live_views&access_token=${access_token}
sample response:
{
"live_views": 1,
"id": "123123"
}
Docs for endpoint 1: https://developers.facebook.com/docs/graph-api/reference/live-video/
Docs for endpoint 2: https://developers.facebook.com/docs/graph-api/reference/live-video/reactions#Reading
However I understand the confusion since live_views is not listed under the second endpoint as a reaction nor even is it a reaction, so either it a mistake in facebook docs or a mistake in the graph API, either the solution provided works.
Yes you do need to go through an app review to get the permission for tracking streams

How to get data of post on facebook like comments, picture by graph api

I using graph api to post picture to wall of user, after get postid i try to get post data by graph api follow this url:
https://graph.facebook.com/v2.4/postid?access_token=my_access_token
But result return only has message, created_time, id
{
"created_time": "2015-09-10T01:52:18+0000",
"message": "test",
"story": "Minh D\u01b0\u01a1ng added 2 new photos to the album: test_demo_app Photos.",
"id": "507382632760376_508325799332726"
}
So, how to get other data of post such as comments, picture in story, likes ?
Please have a look at the docs first. Since v2.4 you have to specify each field you want to be returned from the Graph API. See
https://developers.facebook.com/docs/apps/changelog#v2_4
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.4#fields
Declarative Fields
To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.

How to get Album from facebook fan page

I need get all photos shared by fans in my facebook page fan. I can do?
I try using api graph but I see only album shared by me.
https://graph.facebook.com/inpesca/albums/
You can use a filter to get at some specific types of posts on a wall:
https://graph.facebook.com/inpesca/feed?type=checkin
See "Searching" in the Graph API Reference.
However, you can not use type=photo to get photo posts. For whatever reason, the API doesn't support this (privacy reasons maybe; I don't know). However, each post on a feed has a type field, indicating the type of the post. You can use this to read the feed and filter posts that have type "photo":
// pseudocode
posts = graph.get("/inpesca/feed")
for post in posts:
if post.type == "photo":
// process photo post
else:
// ignore it
To get at the actual picture, access the picture field:
// psuedo code
url = post.picture // "https://fbcdn-photos-a.akamaihd.net/..."
If you look at the URL this points to, it most likely ends with _s.jpg. This is the "small" version of the picture. Replace it with _o and you will get the original version (as far as I can tell this is not documented, but seems to be how it works). You can manually download the photo with curl if you're on a Linux or Mac machine:
curl -O [url]
Or use your programming language's HTTP client library to download it.

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