Background
I have an web app that enables its users to share individual items (posts) on Facebook. I set up a Facebook App to have a API key to do that with the Javascript SDK. It works well and I see the sharing stats in the Facebook App Insights.
The items people share on Facebook are photos and videos, and I'd like to track the total view count, including Facebok shares. Is it possible ?
What I tried
The graph API endpoint /{post_url}
Only gives the shares, comments and likes count
The graph API endpoint /{object_id}/insights/post_impressions
No good, it says the object does not exists *
(*) The docs specify "Only page posts" for that metric, so I guess this is not possible after all, as those posts are not on my App Page feed.
Workarounds
For videos, I guess I can watch the access logs?
About the the images, it seem they are cached in Facebook servers, right? Or does the image file gets hit on my server whenever there is a post view? Any possible way to get a cache invalider for the image so they get hit at each view?
Inferring the average number of view based on the other metrics. For example average_views = shares x A + comments x B + likes x C but that seem very approximate, and I don't know the most adequate values for A, B and C.
Thanks for your help
For a Keyword
Use the open graph API. Here is a live example querying how many likes "Coca Cola" has.
https://developers.facebook.com/tools/explorer/?method=GET&path=cocacola%3Ffields%3Dlikes
Which boils down to:
https://graph.facebook.com/cocacola?fields=likes
Which you could do in an AJAX GET Because of CORS Policy of browser.
The result is:
{
"likes": 71717854,
"id": "40796308305"
}
For a Url
https://api.facebook.com/method/fql.query?query=select%20%20like_count%20from%20link_stat%20where%20url=%22http://www.code.shouttoday.com%22
Also http://api.facebook.com/restserver.php?method=links.getStats&urls=http://code.shouttoday.com will show you all the data like 'Share Count', 'Like Count' and 'Comment Count' and total of all these.
Change the URL (i.e. http://code.shouttoday.com) as per your need.
This is the correct URL, I'm getting right results.
PHP Ajax Request
You Can Show Facebook Share/Like Count Like This: ( Tested and Verified)
$url = http://code.shouttoday.com // You can use inner pages
$rest_url = "http://api.facebook.com/restserver.php?format=json&method=links.getStats&urls=".urlencode($url);
$json = json_decode(file_get_contents($rest_url),true);
echo Facebook Shares = '.$json[0][share_count];
echo Facebook Likes = '.$json[0][like_count];
echo Facebook Comments = '.$json[0][comment_count];
Related
I've followed the guide here https://developers.facebook.com/docs/plugins/oembed/ and it works.
My question is, how can I get my post urls dynamically? so I can repeatedly use this endpoint for the number of posts I have
https://graph.facebook.com/v8.0/instagram_oembed?url={postUrl}&access_token={access_token}
The way the docs show is to hard code a url into the endpoint url, which seems redundant
There is currently no way to get all post URLs for an Instagram profile using the oEmbed API based on the documentation (or anywhere else on the web). You'll have to get all of the post shortcodes (links) by:
Do a GET request to https://www.instagram.com/{profile_handle}/?__a=1. There is a very strict rate limit these days, so it is highly recommended to cache the results the first time, maybe with local storage.
The result will be a JSON object with all of the profile info if the profile is public. You can check of the requests profile is public by checking the [data].graphql.user.is_private
For each [data].graphql.user.edge_owner_to_timeline_media.edges.node, get the shortcode property.
Finally, create your dynamic post URI https://www.instagram.com/p/{shortcode}.
Alternatively, you could do a GET request to `https://www.instagram.com/{profile_handle} and use regex to get the a similar JSON object containing profile data in the HTML response like so:
const uri = `https://www.instagram.com/${handle}/`;
const instaRes = await axios.get(uri);
const html = instaRes.data;
const regex = /_sharedData = (.*);<\/script>/m;
const json = JSON.parse(regex.exec(html)[1]);
The rate limit is strict here too so use be sure to cache. Testing can be a pain because you may have to change your IP address a couple times when you hit the rate limit.
I'm trying to download every comment on a public facebook post (it's one of those "can you do this basic algebra problem" posts - I want to see what percent of the comments get it right).
The Graph API Reference shows that I should be able to just GET graph.facebook.com/v2.5/{object-id}/comments.
I believe the object-id of this post is {user-id}_{post-id}, where post-id is the id in the url. So given this url:
https://www.facebook.com/beth.mansfield.9/posts/10207885721596563
The user-id of facebook.com/beth.mansfield.9 is 1101752663 (from findmyfbid.com), and the post-id is 10207885721596563 (from the url), which makes the object-id="1101752663_10207885721596563".
When I try graph.facebook.com/v2.5/1101752663_10207885721596563/comments in the Graph API Explorer, though, I get:
{
"data": [
]
}
What am I doing wrong? Is there another way to get the comments? There are close to a million so loading them all in the browser and scraping with javascript would be unfeasible.
That is a user profile. You can only get data of a user profile if that specific user authorized your App. In that case, you would need to authorize with the user_posts permission. Just because it is public, does not mean you can get the data - that would only work for Pages.
I am able to get all the page posts that can be promoted via the endpoint given at https://developers.facebook.com/docs/marketing-api/campaigns/objectives#connection_objects
eg query:
https://graph.facebook.com/v2.5/[PAGE-ID]/promotable_posts?fields=id&is_published=true&access_token=[TOKEN]
but how do I get all the posts that are already promoted? I am unable to find anything on the FB API docs.
There is no simple way to do this as there is no association between a post and an ad.
The easiest way to do this would likely be to request all creatives for an account, along with their object_story_id:
/<VERSION>/act_<ACCOUNT_ID>/adcreatives?fields=object_story_id
And then request all ads within the account along with creative and reference whether there is a match:
/<VERSION>/act_<ACCOUNT_ID>/ads?fields=id,creative
This is confusing. So just to clarify:
REQ #1: To fetch basic stats for a URL, you send GET request to:
http://graph.facebook.com/?ids=http://some.com
(alternatively, FQL can be used to fetch stats for a URL, but that doesn't return the OpenGraph Object)
REQ #2: To fetch comments for a URL, you do a GET:
http://graph.facebook.com/comments/?ids=http://some.com
REQ #3: To fetch likes for a URL, you GET:
http://graph.facebook.com/likes/?ids=http://some.com
But how do you comment on / like a URL programmatically?
I guess, likes can be created using Open Graph API, right?
https://graph.facebook.com/me/og.likes?object=1234
where 1234 is the OpenGraph Object ID of an article (as returned by REQ #1).
But this requires an approval process, the like action has to be approved by Facebook.
Is there a better way to do this? Can I use for example the Graph API for all these things?
My goal:
Currently I'm using the Facebook like button and comments plugin to create likes and comments. But these use the JS SDK, which is huge, and they generate a lot of external requests. I wanna get rid of them and just send an AJAX request to my server, which would then asynchronously talk to Facebook, and it would post the Like / Comment.
Is this possible?
Thanks in advance!
I read through the Facebook docs briefly and I don't believe you can do this other than the way you indicated with authenticating.
You should also take a look at this thread: 'Like' a page using Facebook Graph API
You would need to authorize every single user before he would be able to like something, and you would need to go through a review process on Facebook. And i am pretty sure you would not get the required permissions approved just because you want to get rid of the JavaScript SDK overhead, to be honest.
The Social Plugins are asynchronously, so the overhead for downloading the SDK is irrelevant as it happens in the background and it is non-blocking.
I have an idea, to do this. You can use long term access token, Once you login you receive short term token. After receiving short term token you need to request your long term access token. Save that token in DB or file.
Then you can use Graph Api, to make requests. This will eliminate the need for access requirement every time you request api.
Just use access token you saved before.
Refer this documentation from Facebook for further clarity.
https://developers.facebook.com/docs/facebook-login/access-tokens
Happy Coding!
Atul Jindal
Beginner here, and I've been getting lost in the Facebook developer docs and Google for hours. I'm sure this is a simple question, but I just need some direction.
What I'm trying to do: query latest post of a page owner from a public Facebook page with JavaScript and parse it to display within my own HTML (can't use a Social Plugin - I need custom control over HTML/CSS).
What I've got working so far:
var token = '<my_token>';
var query = 'fields=posts.limit(1)';
var request = 'https://graph.facebook.com/[mypage]?' + query + '&access_token=' + token;
$.getJSON(request, function(response) {
alert(response.posts.data[0].message);
});
This does work, however, the Access Token debugger says my access_token is going to expire in 2 months. Why? It's a public Facebook page, and I only want to query the page owner's latest comment. Do I really need to create a Facebook App and login via PHP to just to access this public information?
I'm doing the exact same thing with a Twitter feed and all I had to do was $.getJSON http://api.twitter.com/1/statuses/user_timeline/.json, completely in JavaScript.
It seems getting similar information from Facebook is much more difficult, but perhaps I'm going about it the wrong way?
This does work, however, the Access Token debugger says my access_token is going to expire in 2 months. Why?
Because that’s what user access tokens do.
Do I really need to create a Facebook App and login via PHP to just to access this public information?
For pages that are restricted in any way (based on age, country or for alcohol related content) you have to use a user access token, because that’s the only way Facebook can figure out whether you’re actually allowed to see the content or not.
If it’s your own page, then you could generate a page access token – those don’t expire by default, if you use a long-lived user access token to get them.
But you don’t want to expose that kind of token in client-side JavaScript, because everyone visiting your site could steal it from there and act on behalf of your page then.