How to delete a facebook feed post/comment in php? - php

I want to delete a facebook post or comment that is on my facebook feed. I tried to follow facebook docs,but no in vain.
Here is my code for post delete request.
$postid = "2512732972186856_2511181632341990";
$token = facebook user access token
$feed = $this->facebook->deletes($postid,array (), $token);
When I run this query it says :
{
"error": {
"message": "(#3) Publishing comments through the API is only available for page access tokens",
"type": "OAuthException",
"code": 3,
"fbtrace_id": "A9fyXxLUY3ui9S3CqiCuIDJ"
}
}
However it is important to mention that i didn't create post in page, my post is in my facebook feed.

You cannot create or delete posts on your Profile, you can only create posts on a Page, and you can only delete posts made by your App.
The error message actually tells you that those actions are only available for Pages (with a Page Token).

Related

Facebook PHP - posting error to timeline but not to page

I am trying to post to a Facebook account (with permissions) using the PHP
API. I am giving our users two options - posting to the Feed, and posting
to a specific facebook page. I am getting an error in the first case, but
not in the second case. In the code below:
$access_token is the access token I got back from linking to my app. It's of type
"User", and has the following permissions:
email
pages_show_list
business_management
pages_read_engagement
pages_manage_metadata
pages_read_user_content
pages_manage_ads
pages_manage_posts
pages_manage_engagement
public_profile
Where $page_token is, well, a "Page" type token, with the same set of permissions.
When I post to the page, it works fine. But when I try to post to the timeline, I get
the error:
Graph Error 200 : [Timeline]: [(#200) If posting to a group, requires app being
installed in the group, and either publish_to_groups permission with user token,
or both pages_read_engagement and pages_manage_posts permission with page token;
If posting to a page, requires both pages_read_engagement and pages_manage_posts
as an admin with sufficient administrative permission]
I'm not trying to post to a group, I'm posting to an account's timeline. And, per the
access token debugger, I have the permissions requested anyway. What's going on?
My code is below:
$linkData = [
'link' => $link_to_post,
'appsecret_proof' => $app_secret_proof
];
if ( POSTING TO TIMELINE )
{
$token = $access_token;
$post_to = "/me/feed";
}
else
{
$page_id = $page_id;
$token = $page_token;
$post_to = "/$page_id/feed";
}
// THIS THROWS THE EXCEPTION:
$response = $this->fb->post($post_to, $linkData, $token);
You can not post to a personal timeline via API any more, that was removed ages ago already. (With introduction of API v2.4, if I remember correctly.)
You can only use the Share or the Feed dialog, to offer the user a way to actively share / make a post themselves, https://developers.facebook.com/docs/sharing/web

API facebook don't publish feed on page

I want to publish feeds on the Facebook Page by API. I have followed the facebook documentation creating app in the facebook developer. Before I have registered a account facebook (example: test_account) and after that I have created the page for the business activity (example: test_page) where the manage admin is "test_account".
I have gone to the API by Graph API Explorer
https://developers.facebook.com/tools/explorer
I have granted the "publish_actions" and taken the access token generated by interface tool as followed image
After that I have tried the call POST request to test the publish feeds on the page facebook (test_page) by
https://graph.facebook.com/{id_page_business}/feed?message=test_message&access_token={access_token_APP}
and I have received the new ID_feed and I have seen the feed on facebook page and OK, but the access token the day after was expired. How can I have the access token that don't expired ?
I have tried another solution to obtain the access token from the call GET request (as documentation)
(STEP 1)
so I have obtained access token associated to the APP (see below)
access_token={APP_ID}|xxxxxxxxxxxxxxxxxxxxxjw
(STEP 2) I have sended call GET request
https://graph.facebook.com/{APP_ID}/accounts?access_token=APP_ID}|xxxxxxxxxxxxxxxxxxxxxjw
and I have obtained the followed response:
{
data: [1]
0: {
id: "xxxxxxxx"
login_url: "https://developers.facebook.com/checkpoint/test-user-login /132173517148280/"
access_token: "123456........"
}
paging: {
cursors: {
before: "yMTczNTE3MTQ4Mjgw"
after: "yMTczNTE3MTQ4Mjgw"
}
}
}
(STEP 3) So I have tried the call POST request to publish the feed on the facebook page with the last access token (123456........) and I have received
{
id: "1256xxxxxx_133346xxxxxx"
}
but in the page business I don't see the feed... WHY?
IMPORTANT:I have noticed if I call the request GET API to obtain all feeds page (https://graph.facebook.com/ID_PAGE/feed?access_token=CAAPG1DFdxrs...........) I can see the feeds published, but into the facebook interface timeline I don't see the feed.
I have noticed that the feed publish by API return:
message: "test"
created_time: "2015-11-29T16:39:44+0000"
id: "APP_ID_138999329797136"
The second part the id is different respect when I publish the feed with access token get from the API Graph.
You need to have extended token on page access token to post on the page. App access token will provide you all read access to the page but will not allow you to post.
Below steps must help -
Get the page access token using -
https://graph.facebook.com/{APP_ID}/accounts?access_token={APP_ID}|xxxxxxxxxxxx
Using the page access token obtain the extended token, below is how I had implemented using VB.NET
Dim parameters As Object = New ExpandoObject()
parameters.client_id = FB_APP_ID
parameters.client_secret = FB_APP_SECRET
parameters.grant_type = "fb_exchange_token"
parameters.fb_exchange_token = short_token 'FBClient.AccessToken
Dim result As Object = FBClient.Get("oauth/access_token", parameters)
Use this extended token to post on page. It worked for me, hope it helps you.

How can I automatically scrape new information from page with Facebook open graph API

Hi I have this problem with Facebook open graph API.
I know I can manually scrape new information about meta tags from a page with https://developers.facebook.com/tools/debug/. But I'm trying to use api to do it automatically, but I just can't understand what is going on.
I stumbled across this https://developers.facebook.com/tools/explorer/145634995501895/. Can you guys tell me exactly what to type there to scrape new og meta tags?
For page: http://www.akademiaharmonia.pl/o-jodze.html
What I tried: in this Graph API Explorer I chose "POST" and added 2 fields:
id = http://www.akademiaharmonia.pl/o-jodze.html
scrape = true
I got this error:
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
I don't really understand why but it works perfectly fine now. I also noticed that the graph explorer is now 2.1 instead of 2.0... hmm...

Facebook Graph API v2.1: getting user id and his posts

Imagine the situation when I'm logged in facebook web application as userA. I know userB's nickname but he is not in my friendlist and I do not know his id. And, of course, he does not use my app.
How can I get userB's id via a nickname?
After that how can I get his wall posts?
Are there any specific permissions? May be there are manual pages I've missed?
In v1.0, the following was possible:
http://graph.facebook.com/v1.0/{user-name}
However, with v2.0 and v2.1, accessing users via IDs or Usernames not connected to your application is not possible. E.g., trying http://graph.facebook.com/v2.1/{user-name} will throw an error:
{
"error": {
"message": "(#803) Cannot query users by their username (user-name)",
"type": "OAuthException",
"code": 803
}
}
Using the new PHP SDK (v4.0.x) you can do the following:
$response = (new FacebookRequest( $session, 'GET', '{user-name}', array(), 'v1.0' ))->execute()->getGraphObject()->asArray();

Bring in a facebook users stream into a webpage

I need help on how to bring in a users personal stream.
For example when I go to this webpage I want the user to be able to see his/her facebook live profile stream as if he/she was looking at the facebook website.
Also would you recommend I use the Javascript or PHP SDK ?
Any help or links to examples would be much appreciated.
To get the user's stream an app needs permission to get this data
When a user logs into an app, the app gets access to their public profile and friend list - to read more info about a person, an app must request additional permissions from them
Permissions enable you to access user information. The Graph API reference provides detailed information on the kind of information stored in a user profile and the specific permissions that must be granted by the user before your app can access it.
http://developers.facebook.com/docs/concepts/login/permissions-login-dialog/
In order to be granted permission you need an access token
An access token is a random string that provides temporary, secure access to Facebook APIs.
A token identifies a User, App or Page session and provides information about granted permissions. They also include information about when the token will expire and which app generated the token. Because of privacy checks, the majority of API calls on Facebook need to be signed with an access token
For a user profile, you need a user access token
User access tokens are the standard type for API calls; these are generated in the login flow when a user grants permissions to an app.
Once these basics are known, then you need to pull the data via an HTTP GET to
https://graph.facebook.com/me/feed?access_token=USER_ACCESS_TOKEN
Which will return an array of Post objects containing (up to) the last 25 posts.
http://developers.facebook.com/docs/reference/api/user/#feed
Now you have a JSON response that looks similar to this
{
"data": [
{
"id": "13608786_10101118139895617",
"from": {
"name": "Philippe Harewood",
"id": "13608786"
},
"picture": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDuPlCx0L1BP7wp&w=130&h=130&url=http%3A%2F%2Fi4.ytimg.com%2Fvi%2F7_3hKVxOcRI%2Fmqdefault.jpg",
"link": "http://www.youtube.com/watch?v=7_3hKVxOcRI",
"source": "http://www.youtube.com/v/7_3hKVxOcRI?version=3&autohide=1&autoplay=1",
"name": "Suit & Tie (Acoustic Cover) - Tori Kelly",
"description": "hope you enjoy my cover of Suit & Tieeee. JT is the man. for all info and tickets to my upcoming shows go to torikellymusic.com !! love you guys! -----------...",
"icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yj/r/v2OnaTyTQZE.gif",
"privacy": {
"value": ""
},
"type": "video",
"application": {
"name": "YouTube",
"namespace": "yt-fb-app",
"id": "87741124305"
},
"created_time": "2013-03-23T23:21:50+0000",
"updated_time": "2013-03-23T23:21:50+0000",
"comments": {
"count": 0
}
}
],
"paging": {
"previous": "https://graph.facebook.com/13608786/feed?limit=25&since=1364080910",
"next": "https://graph.facebook.com/13608786/feed?limit=25&until=1364080909"
}
}
Based on the language of your app, you should be able to the parse response correctly. In this case to have a near persistent stream of feed data from a user profile. You will need t o store the user access token so that the app can request access any time.
Normally a user access token only lasts for two hours so extending the token to last two months will be a wise choice. You can extend the token by using the following call
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
You can exchange this token for a longer-lived one (that will valid for up to 60 days) by passing it to the /oauth endpoint from your server (so that the secret is not exposed) with a grant_type parameter of fb_exchange_token
Now you should store this in a database, e.g. MySQL for retrieval later.
So in summary, load your SQL
CREATE TABLE IF NOT EXISTS `facebook_data` (
`ID` int(11) NOT NULL,
`access_token` varchar(255) NOT NULL,
UNIQUE KEY `ID` (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Pre-populate the data if you wish
INSERT INTO `facebook_data` (`ID`, `access_token`) VALUES
(1, 'temptoken');
Normally I tend to allow JS SDK to handle the user authentication and PHP SDK to handle the API calls. At the same time allowing the cookie set by the JS SDK to be picked up by the PHP SDK
Once the PHP SDK is set up with the cookie and has parsed the signed request
I will extend the access token and the set the new access token in the database.
$facebook->setExtendedAccessToken();
$fbdb_result = $fbdb->query("UPDATE facebook_data SET access_token='" . $facebook->getAccessToken() . "' WHERE ID = 1");
Now in my presentation page, whenever I need I will pull in the access token from the database and set the PHP SDK to the correct user.
$result = $fbdb->query("SELECT access_token FROM facebook_data WHERE ID = 1");
$access_token = mysql_result($result, 0);
$facebook->setAccessToken($access_token);
Then I ensure that no invalidation has occurred since I last stored it
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
$user_posts = $facebook->api('me/feed'); // The user's feed on their wall
}
Then start handling each post and displaying it with some styled CSS
foreach($user_posts['data'] as $post){
$post_link = $post['actions'][0]['link'];
$page_id = $post['from']['id'];
$page_name = $post['from']['name'];
$message = ($post['message']) ? $post['message'] : " ";
$name = ($post['name']) ? $post['name'] : " ";
$story = ($post['story']) ? $post['story'] : " ";
$post_time = $post['updated_time'];
}
You can create a badge for your site (the gear wheel at the top-right of your FB profile) and click on "edit badge". There are quite a few options available.

Categories