Facebook PHP - posting error to timeline but not to page - php

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

Related

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.

Facebook Php Api basics

Im getting started with the php sdk, and struggling to understand a few things (I have a basic example below - but everytime the pic goes to MY wall and not the fan page)
Code:
require_once("facebook.php"); // set the right path
$config = array();
$config['appId'] = 'app id';
$config['secret'] = 'app secret';
$config['fileUpload'] = true; // optional
$fb = new Facebook($config);
$params = array(
// this is the access token for Fan Page
"access_token" => "I create this via the graph api (select App and click get token), I select publish stream and photo_upload permissions.",
"message" => "test message",
"source" => "#" ."photo.png", // "#" .
);
try {
// **** is Facebook id of Fan page
$ret = $fb->api('/****/photos', 'POST', $params);
echo 'Photo successfully uploaded to Facebook Album';
} catch(Exception $e) {
echo $e->getMessage();
}
the picture keeps going to MY wall (logged in user) and not the fan page, is that because im getting a token for the currently logged in user (me) instead of the fan page? and if so how do i generate a token for the fan page whilst logged in as the developer? (is it by putting the fan page id into the Get bar?)
Apologies for the noob questions but the facebook documentation sucks (until u understand the core functionality of course). Most tutorials work once u know how to use the graph API - none ive looked actually EXPLAIN how to use the graph to generate correct tokens, etc.
\POST /<ID>/photos — this will post on the <ID>'s wall only, so please double-check the <ID> that you are using.
The access token will depict that on behalf of whom the photo would be posted on the <ID>'s wall. If you use the user access token, the photo will be published on behalf of the user and if page access token is used, it will be published on the page's behalf itself.
You can get the page access token of your pages by-
\GET /me/accounts?fields=access_token
(permission required: manage_pages)
Demo

How to retrieve user's newsfeed list (VK.com)?

I am using following php code :
$vk = new VK($app_id, $api_secret);
$user_wall = $vk->api('newsfeed.get', array(
//'owner_id' => $o->profile_uid,
'count' => 100,
'filters' => 'post,photo,wall_photo,friend',
'access_token' => $o->profile_token
));
echo '<pre>';
print_r($user_wall);
exit;
I am getting error when trying above code. I have successfully completed auth and stored user profile info in mysql table. I notice that when I see Api.Console permission in App> Setting, I see Access the Wall permission. But in application I used to retrieve data, I do not see this permission.
Error description : Permission to perform this action is denied
Error code : 7
The documentation is poorly described. Even which field is required or optional I can not determine. And what is difference between wall.get with filter "others" vs newsfeed.get ?
LOGIN CODE:
$AuthURL = $vk->getAuthorizeURL('notify,friends,photos,audio,video,docs,notes,pages,status,offers,questions,wall,groups,notifications,stats,ads,offline', $redirect_uri);
AUTH CODE:
$vk_code = $_REQUEST['code'];
$vk = new VK($app_id, $app_secret);
$access_token = $vk->getAccessToken($vk_code, $redirect_uri);
$uid = $access_token['user_id'];
$token = $access_token['access_token'];
$user_info = $vk->api('users.get', array(
'user_ids' => $uid,
'fields' => 'nickname, screen_name, sex, bdate (birthdate), city, country, timezone, photo, photo_medium, photo_big, has_mobile, contacts, education, online, counters, relation, last_seen, activity, can_write_private_message, can_see_all_posts, can_post, universities, counters'
));
First you must register application: vk.com/editapp?act=create
Then you need get authorize code. To do this follow link:
oauth.vk.com/authorize?client_id={APP_ID}&scope={API_SETTINGS}
Where {APP_ID} — your application id (see on app settings page),
{API_SETTINGS} — access rights requested by your app (through comma). If need infinite token use key "offline". For newsfeed need use key "wall,friends,offline".
Opens page. Copy string in URL AFTER #code=
Later you need get access token. Go to link:
https://oauth.vk.com/access_token?client_id={APP_ID}&client_secret={API_SECRET}&code={CODE}
Where {API_SECRET} — secret application key (see on app settings page),
{CODE} — code that was copied in step 2.
Copy access_token.
To get newsfeed data request link:
https://api.vk.com/method/newsfeed.get.xml?access_token={ACCESS_TOKEN}
Where {ACCESS_TOKEN} — token that was in step 3.
NOTE: USE HTTPS WHEN USING THIS ACTIONS
You need the following rights to call this method: wall and friends. (Read more on rights)
You must generate authorization with wall and friends...
https://oauth.vk.com/authorize?client_id=APP_ID&scope=wall,friends,offline
Just replace APP_ID with your appilication and then get your token

Post to facebook page on behalf of user who is not a page admin

We are trying to build a facebook application that posts a message from a user to a facebook business page (which the user is NOT an admin of.) We need the post to be from the USER, not the page or the page admin. We are using the facebook PHP SDK and so far have accomplished getting the user's message to post to their own feed. (Using an App Access Token.) Though when we try to post the message to the facebook page (not owned by the user) we get this error:
Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in
Is it possible to post to a facebook page, not owned by the user on behalf of the user? We are admins on the facebook page, but if we post using the Page Access Token then the post is no longer authored by the user, but instead the facebook page or page admin.
Here is a snippet of our code to see how we're going about this:
Posting to user's feed as user (works)
$fbpage = "/$fbid/feed";
$attachment = array(
'message' => $msg,'from' => array('name' => $name,'id' => $fbid));
$result = $facebook->api('/'.$fbpage.'?access_token='.$accesstoken, 'post', $attachment);
Posting to facebook page as user (doesn't work)
$fbpage = "/johnsjoint/feed";
$attachment = array('message' => $msg,'from' => array('name' => $name,'id' => $fbid));
$result = $facebook->api('/'.$fbpage.'?access_token='.$accesstoken, 'post', $attachment);
After a lot of research, it appears that this isn't possible. If anyone knows otherwise, please post here.
Not sure if its the exact same issue, but this might shed a light over your problem. See
"Posting to Facebook fan page feed as user through PHP SDK"

Authorizing a Facebook Fan Page for Status Updates

I'm able to update the status on my PROFILE wall using this code:
require_once 'facebook-platform/php/facebook.php';
$facebook = new Facebook('APP API KEY','APP SECRET KEY');
$user_id = 'MY USER ID';
$facebook->api_client->users_setStatus('This is a new status');
...after authorizing using this address:
http://facebook.com/authorize.php?api_key=MYAPPAPIKEY&v=1.0&ext_perm=publish_stream
This code, however, does not work to update the status on my Facebook PAGE Wall. Are there additional parameters that I can add to the authorize.php url to specify authorizing the PAGE and not just my profile?
Or, are there better ways to post updates to Fan Page Walls?
Thanks!
I solved the problem by consulting the Facebook desktop application documentation (even though this is a web application).
I first had to authorize offline access with this url (replacing 'MYAPIKEY'):
http://www.facebook.com/login.php?api_key=MYAPIKEY&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&session_key_only=true&req_perms=read_stream,publish_stream,offline_access
Then, I needed to grant 'publish_stream' permissions to the PAGE with this url (replacing 'MYAPIKEY' and 'THEPAGEID'):
http://www.facebook.com/connect/prompt_permissions.php?api_key=MYAPIKEY&v=1.0&next=http://www.facebook.com/connect/login_success.html?xxRESULTTOKENxx&display=popup&ext_perm=publish_stream&enable_profile_selector=1&profile_selector_ids=THEPAGEID
I could then use the following code to publish to the Fan Page wall:
require_once 'facebook-platform/php/facebook.php';
$facebook = new Facebook(MYAPIKEY, MYAPISECRET);
try{
$facebook->api_client->stream_publish('INSERT_STATUS_HERE',null,null,null,'THEPAGEID');
}catch(Exception $o ){
print_r($o);
}
Based on the above, i tried out a couple of querystring parameters on the graph API authorize URL, and it appears this works:
https://graph.facebook.com/oauth/authorize?client_id=[APP_ID]&redirect_uri=[REDIRECT_URL]&scope=publish_stream&enable_profile_selector=1&profile_selector_ids=[PAGE_IDS]
EDIT: Never mind, the above displays all the UI correctly, but still get the "(#200) The user hasn't authorized the application to perform this action" error --- it's clear it doesn't work because the access token contains my USER id but not the page's ID.

Categories