How to get all feeds of my fanpage in facebook - facebook-graph-api

How can I get all feeds of my fan page?
I am using graph api using this url
https://graph.facebook.com/mypagename/feed?until=today&access_token=$access_token
$status_data= file_get_contents("https://graph.facebook.com/mypagename/feed?until=today&access_token=$access_token");
$status = json_decode($status_data,true);
access_token is valid. Everything works fine. I have take permission also.(read_stream)
but it only return 50 to 52 posts.
I want to return all posts.

Have you tried just calling:
https://graph.facebook.com/mypagename/feed?access_token=$access_token
The API should return a fixed default set (most recent 25 posts). However, it should also return a paging object which lets you access the next 25:
"paging": {
"previous": "https://graph.facebook.com/mypagename/feed?format=json&limit=25&since=1317858935",
"next": "https://graph.facebook.com/mypagename/feed?format=json&limit=25&until=1315842409"
Your code can parse the paging links automatically until the end to retrieve all posts.

Related

Facebook's Grap api on {postId}/comments returns empty data on some postIds

When i'm making a call to Facebook's graph api to fetch comments made on a specific ad, I get an empty data back even though the ad has comments made on it. This only occurs on some ads not all so the url must be right.
url on which i make the call:
https://graph.facebook.com/v15.0/${postId}/comments?access_token=${access_token}
The call returns with status code 200 -->
{
"data": []
}

Instagram Graph API - Fetch media insights metric when a user switched from personal to business account

I'm looking for a way to fetch Media Insights metrics in Instagram Graph API (https://developers.facebook.com/docs/instagram-api/reference/media/insights) with a nested query based on the userId, even when a client switched from a Personal to a Business account.
I use this nested query to fetch all the data I need : https://graph.facebook.com/v3.2/{userId}?fields=followers_count,media{media_type,caption,timestamp,like_count,insights.metric(reach, impressions)} (this part causes the error: insights.metric(reach, impressions) - it works however for an account that has always been a Business one)
However, because some media linked to the userId were posted before the user switched to a Business account, instead of returning the data only for the media posted after, the API returns this error:
{
"error": {
"message": "Invalid parameter",
"type": "OAuthException",
"code": 100,
"error_data": {
"blame_field_specs": [
[
""
]
]
},
"error_subcode": 2108006,
"is_transient": false,
"error_user_title": "Media Posted Before Business Account Conversion",
"error_user_msg": "The media was posted before the most recent time that the user's account was converted to a business account from a personal account.",
"fbtrace_id": "Gs85pUz14JC"
}
}
Is there a way to know, thru the API, which media were created before and after the account switch from Personal to Business? Or is there a way to fetch the date on which the account was switched?
The only way I currently see to get the data I need is to use the /media edge and query insights for each media until I get an error. Then I would get approximately the date I need. However, this is not optimized at all since we are rate limited to 200 calls per user per hour.
I have the same problem.
For now, I'm Switch between queries (if first have error)
"userId"?fields=id,media.limit(100){insights.metric(reach, impressions)}
"userId"?fields=id,media.limit(100)
I show the user all insights in zero.
I don't know if they're the best alternative, like identify the time of conversion to business and get the post between this range of DateTime
I got the same problem and solved it like this:
Use the nested query just like you did, including insights.metric
If the error appears, do another call without insights.metric - to at least get all other data
For most accounts, it works and there is no additional API call. For the rest, i just cannot get the insights and i have to live with it, i guess - until Facebook/IG fixes the issue.
I got the same problem and solved it like this:
Step1: Convert your Instagram account to a Professional account.
Step2: Then According to Error Post a new post on Instagram and get their Post-ID.
Step3: Then try to get a request using that Post-ID.
{Post-ID}?fields=comments_count,like_count,timestamp,insights.metric(reach,impressions)
curl -i -X GET "https://graph.facebook.com/v12.0/{Post-ID}?fields=comments_count%2Clike_count%2Ctimestamp%2Cinsights.metric(reach%2Cimpressions)&access_token={access_token}"
For more: insights
Here is the relevant logic from a script that can handle this error while still doing a full import. It works by reducing the requested limit to 1 once the error is encountered. It will keep requesting insights until it encounters the error again, then removes insights from the fields and returns to the requested limit.
limit = 50
error_2108006 = False
metrics = 'insights.metric%28impressions%29%2C' # Must be URL encoded for replacement
url = '/PAGE_ID/media?fields=%sid,caption,media_url,media_type&limit=%s' % (metrics, limit)
# While we have more pages
while True:
# Make your API call to Instagram
posts = get_posts_from_instagram(url)
# Check for error 2108006
if posts == 2108006:
# First time getting this error, keep trying to get insights but one by one
if error_2108006 is False:
error_2108006 = True
url = url.replace('limit={}'.format(limit), 'limit=1')
continue
# Not the first time. Strip out insights and return to desired limit.
url = url.replace(metrics, '')
url = url.replace('limit=1', 'limit='.format(limit))
continue
# Do something with the data
for post in posts:
continue
# If there are more pages, fetch the next URL
if 'paging' in posts and 'next' in posts['paging']:
url = posts['paging']['next']
continue
# Done
break

Missing some dark posts in list of posts facebook graph API

I am facing a problem with retrieving dark post via Facebook graph API.
For example, I call request to particular dark post like this:
/123456789_111112222223333?fields=is_hidden,is_expired,is_published,message,created_time
I receive the requested object like this:
{
"is_hidden": true,
"is_expired": false,
"is_published": false,
"message": "Some message",
"created_time": "2019-03-22T13:35:10+0000",
"id": "123456789_111112222223333"
}
But when I call request for promoted_posts:
123456789/promotable_posts?fields=is_hidden,created_time&include_hidden=true&is_published=false&include_inline=true
I receive an empty array of objects. When I change parameter is_published into true, I receive list of posts, with created time before and after that post.
How can I receive all dark posts for the page? Thanks for any advice.
Easy peasy!
/{page-id}/ads_posts?include_inline_create=true
Gives you all ads posts (Dark Posts also included)

Instagram Business Discovery API Pagination Token not working?

I am trying to use Instagram Business Discovery API to get post statistics from a certain public page.
Take Bath and Body Works (https://www.instagram.com/bathandbodyworks) for example.
GET graph.facebook.com
17895695668004550?fields=business_discovery.username(bathandbodyworks){followers_count,media_count,media{timestamp,like_count,comments_count}}
This will give me the following response:
{
"business_discovery": {
"followers_count": 3526159,
"media_count": 3536,
"media": {
"data": [
{
"timestamp": "2018-05-16T20:00:54+0000",
"like_count": 28925,
"comments_count": 530,
"id": "17917935010179826"
},
(24 posts data omitted...)
"paging": {
"cursors": {
"after": "QVFIUlBNak5fNTc3eThl..." (a very long string)
}
}
}
Now, this only gives me the most recent 25 posts, which I believe is the limit per request set by Facebook.
What should I do if I want to load the next 25 posts?
In YouTube Data API there is also a limit per request of 50, but a "nextPageToken" is provided to load the next 50 post. I assume this is the same case here?
I also found in this Facebook API document that maybe I can add a cursor string like: &after=QVFIUlBNak5fNTc3eThl...,
but this doesn't work.
Any suggestions would be appreciated, thank you!
The pagination with business_discovery is a bit different from Facebook
pagination.
There's no next and previous url's.
You need to add, after cursor as this example:
https://graph.facebook.com/v2.12/xxxx?fields=business_discovery.username(jacqehoward){id,name,username,website,profile_picture_url,biography,followers_count,media_count,media.after(QVFIUlZA5aTR5NTE4Y24tNW90VGZAPTVBtb0NpOEFPTlNLeklmVTEtUDZAfVnE0YnBhUVNOQ3BDaktzNHJBTENhTmVYLUV2SGJPZAVAxZA09hQ2NhUGdnUGFjMTNn){id,caption,comments_count,like_count,media_type,media_url,owner,timestamp}}
Note:
media.after(after_cursor){media_fields}
And if you want to paginate through business_discovery to the end you must add after cursors until there's only response with previous cursor. This is the mark that you reached the first post.

How to scrape user's facebook feed using Graph API?

I'd like to scrape some user's posts from Facebook. Posts are visible in browser, when I'm logged in and I am looking at http://fb.com/username URL. But when I'm trying to access the same feed using Graph API Explorer (https://graph.facebook.com/v2.3/username/feed) I'm getting:
"error": {
"message": "(#803) Cannot query users by their username (username)",
"type": "OAuthException",
"code": 803
}
If I try to use id instead of username (http://graph.facebook.com/v2.3/userid/feed) I'm getting empty JSON in response:
{
"data": [
]
}
So, is there a way to get the user's feed using Graph API? Or do I have to imitate a web browser and parse HTML then?
You are not supposed to grab the feed of any user, public or not. You need to authorize a user with user_posts to get access to his feed - and his feed only.
Also, usernames are not available in the Graph API anymore, you need to use an App Scoped ID now. You get that when you authorize the user.
Btw, scraping is not allowed at all: https://www.facebook.com/apps/site_scraping_tos_terms.php