Javascript Facebook SDK Add/Delete comment on Users feed. Access Token - javascript

I have a web app running on a local server that I can log into Facebook through.
I have been trying to add or delete comments from Posts are made by me and on my timeline.
When I try to execute the FB.api() call I get a failed response saying that I need a Page Access Token. The thing is, I've been looking around a lot and have seen nowhere a mention of a Page Access Token for a specific Persons profile.
Through experimenting I've been able to:
Get a Page Access Token for a non-profile Page that I own
Add/Delete Comments/Posts on that non-profile page
Add/Delete Posts made by the app on my profile
Display all Posts/Comments on my profile
What I want to do:
Be able to delete/add comments on Posts on MY profile
What I've tried:
GET request for {my-id}?field=access_token. I figured that if it worked for a non-profile page, it would work for my profile. It didn't work.
A lot of googling
My Assumptions:
I need a Page Access Token for my own profile if I want to add/delete things that were not made by the app
A Page Acess Token for a profile exists.
Below is an example of a FB.api call that would return a response saying:
"Publishing comments through the API is only available for page access tokens"
FB.api(`${comment_id}`, 'DELETE', function(response) {
console.log(response);
});
I really just need someone to give me some concrete answers.

Related

Getting empty list on post reactions. What could be the reason?

I am trying to read reactions on a post using the following graph-api call:
FB.options({version: 'v2.12'});
FB.setAccessToken('EAACEdE7w4Fp85PlhH0NYpZBddHyqkx6XXXX')
FB.api('/765898450131958_1518951084826687/insights?metric=page_actions_post_reactions_wow_total', (res) => {
console.log(res)
});
But I always receive an empty data list. What could be the reason for this? For reference I am using FB module to connect to graph-api.
The acces token I am using is my page-access token
In my case, the facebook app instance wasn't set to live. Once the facebook app instance is live, data array returns user info list with id and name . You can check that out in App Review Settings
Click your app instance -> Go To "App Review" -> Click "My Permissions & Requests".
You will see that there are two approved items. I think these are added or granted by default. But if your app is not live, the two permissions will not be active. That includes basic user information like name and profile picture.
TL;DR - Check if your app is live and also make sure that you are using Page Access Token rather than User Access Token.
From official Facebook API documentation:
On February 5th, 2018, User information will not be included in responses unless you make the request with a Page access token. This only applies to Comments on Pages and Posts on Pages.
https://developers.facebook.com/docs/graph-api/reference/v2.12/object/reactions

Get a page access token with Facebook graph api

I am using angular-facebook to login users to my app using facebook. Here is the code that logs users in:
Facebook.login(function (response) {
$scope.access_token = response.authResponse.accessToken;
});
and then in order to get the facebook id of the user I send a request like this:
Facebook.api('/me', function(response) {
$scope.user = response;
}
Then I save this user in my database.
After this, in $scope.user.id, I have let's say ID abcd.
Then I use this id to get the page access token by sending a request to the following url:
https://graph.facebook.com/'+$scope.user.id+'/accounts
This is giving me this error:
Unsupported get request. Please read the Graph API documentation
Then, in order to check whether my ID is correct, I manually typed the following url in the browser:
https://graph.facebook.com/me?access_token=123123123asdas
To my surprise, I get a different ID here, let's say, 'xyz!'
What am I doing wrong? Why did I get different IDs?
I found my mistake.
As manage_pages permission should be reviewed first, I was getting the access token with manage_pages permission from graph explorer. As it turned out, using different access_tokens will give you different user IDs. So, I will have to get my app reviewed.
This different ID thing is because of app_scoped IDs Facebook introduced recently.

How to use my long (60day) access token to get user data from facebook (offline)

I cannot find any example on facebook dev. or here on stackoverflow so I need to make this post which i didn't thought i had to.
I have a website which uses the facebook JavaScript SDK to login users into it.
When I log in users I uses the
FB.login(), scope:{'email, offline_access'}
When the login in done i get the short lived access token and passes it to:
https://graph.facebook.com/oauth/access_token?
grant_type=fb_exchange_token&
client_id=APP_ID&
client_secret=APP_SECRET&
fb_exchange_token=SHORT_LIVED_ACCESS_TOKEN
So i get the long lived, 60day, token in response. This is done server-side and i save the new long lasting token in my database along with facebook id etc.
Now to my problem. Every day at a certain time I want to update the users data in my database using the access token, which i have read that could. But how do i do that?
I'm used to do like this after the FB.login() but now when the user do not login I don't know how to do the call to FB.api using my persisted token?
FB.api(/me....)
Regards,
Kristoffer
As you have to synchronize/update information when user is not logged in you will have to use server side interaction.
You may use your saved Extended Access Token and set it into Facebook PHP SDK using setAccessToken() method.
$facebook->setAccessToken($extended_access_token);
After this you may call $facebook->api(...) method to perform the task that you intend to.

GET Facebook wall posts of admin

What started off as easy as "Let's put all our brand's Facebook posts on our website" has turned out much more difficult.
The situation:
There are multiple admins that manage the Facebook fan page (20+)
RSS feeds no longer seem to work with some brand pages
I have registered an APP ID, and am getting back an access token at an application level like so:
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=AAAAAAAA&client_secret=BBBBBBBB;
Below are some varying cases and what they return. All examples below assume the access token is retrieved using the URL above
Case 1: /posts
Will return the wall posts of the parent account (the one who started the Facebook fan page), and not the admin's who administer it.
https://graph.facebook.com/PAGE_NAME/posts?access_token=XXXXXXXXXX;
Case 2: /feed
Will return the wall posts by all other users, and NOT the admins or page owners
https://graph.facebook.com/PAGE_NAME/feed?access_token=XXXXXXXXXX;
Case 3: FQL
Will return same results as Case 1, note DDDDDDD = Page UID
SELECT post_id, created_time, message FROM stream WHERE source_id=DDDDDDDD AND actor_id=DDDDDDDD
Now, funnily enough, when I use Facebook's Graph API Explorer (logged in of course), and I call Case 1 above "/posts" I DO get all the posts (including the ones from all page admins and owners). This is exactly what I want!
So why am I here? Because of the access token issue. This is a stream to be used on a website, i.e. there is no requirement for a user to connect to Facebook as we don't need their permissions nor to access their data, which is why I'm using the application access token.
Is manage_pages permission given to your app token? I hope this link helps: http://developers.facebook.com/docs/authentication/pages/

Explain the Facebook access_token

This whole Facebook access_token thing is driving me nuts. All I want to do is fetch a user's public Facebook posts.
It used to work by simply doing:
$.getJSON('http://graph.facebook.com/USERNAME/posts?limit=LIMIT&callback=?', function (posts) {
// Posts available in "posts" variable
});
But now I get the "access_token required" error.
Trust me; I've checked the docs, Googled all over and checked similar questions on SO but I'm really hoping there's a more straight forward way of doing this than what I've seen.
Do you really have to create a Facebook App (I don't even have an account), make the user "accept" the app and login etc for this to work?
With Twitter it's just as easy as it used to be with Facebook.
You really have to create a Facebook App (You need to have an account), make the user "accept" the app and login etc for this to work.
However you can search public posts of user (not a specific user) by facebook's public search api.
e.g.
https://graph.facebook.com/search?q=hello&type=post
This will search all posts with hello keyword in it.
Reference ( you need to have facebook account to see this page )
http://developers.facebook.com/docs/reference/api/
Edit (after seeing comments):
If you want to access public posts of your own page. You can pull it without any login from user (but you will need an access_token)
Generate an offline access_token from here,
http://developers.facebook.com/tools/explorer.
Then you can use that token to pull data . Thus no need of logging on by user.
https://graph.facebook.com/wesellwine/posts?access_token=<access_token from graph api explorer>

Categories

Resources