Facebook iFrame app PHP SDK 3.1.1 - post on the wall - facebook

I'm using facebook PHP SDK 3.1.1, an iFrame app can post on the logged on users wall who is already a fan of the page.
Following is the coe and it gives me error "Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in"
/*post starts*/
$attachment = array('message' => 'test message',
'name' => 'test app!',
'caption' => "Caption of the Post",
'link' => 'http://apps.facebook.com/phpsdk_demoapp/',
'description' => 'It is fun!',
'picture' => 'http://www.takwing.idv.hk/facebook/demoapp_phpsdk/img/logo.gif',
'actions' => array(array('name' => 'Start Learning',
'link' => 'http://www.takwing.idv.hk/tech/fb_dev/index.php'))
);
$result = $facebook->api('/me/feed/',
'post',
$attachment);
/*post ends*/
I guess the part that takes permission from the user to post on his wall must be added to it, kindly help.
Thanks

You have to get publish_stream permission: http://developers.facebook.com/docs/guides/policy/examples_and_explanations/Extended_Permissions/

Before user accessing your app you have to get user permission to wall post. This can be achieved by
http://www.facebook.com/dialog/oauth?client_id=" . <your app id> . "&redirect_uri=" . <app redirect url> . "&scope=publish_stream,offline_access'
This will give you access to post on users wall.

Related

How to enable share_item on a Facebook Page?

We have several FB Pages and are needing to post items to the pages. We're getting the error Fatal error: Uncaught OAuthException: (#282) Requires extended permission: share_item.
I had the same error when I first posted to my personal FB page. But worked after I checked the "share_item" in https://developers.facebook.com/tools/explorer
The issue is, how can I enable "share_item" on other FB pages that I'm a "manager" to? Is there a page in the FB Menu that I'm not seeing or how do I use https://developers.facebook.com/tools/explorer with those other pages to enable it?
The following code is what works once I've enabled the "share_item" on my personal FB page, however again the question is how do I enable share_item on a FB page that I'm a manager of?
$fb = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_SECRET,
));
$fb->api( '/' . FB_PAGE_ID . '/links', 'POST', array(
'client_id' => FB_APP_ID,
'access_token' => $fb->getAccessToken(),
'link' => 'http://www.example.com',
'message' => 'Some comments...'
));
Read the Permission documentation again - your Authentication flow must not be requesting the correct permissions from the users.
Request publish_stream permission - this encompasses all the other publishing permissions like share_item, status_update, etc.
If this is a Page access token you're using it should already have the appropriate permissions

Application post to its Wall

I can't find an example of exactly what I'm trying to do, so sorry if this is a repeated question, but I have looked for the last couple of hours.
I want my Facebook app to be able to update a status or post to a wall of a page that I administrate: http://www.facebook.com/amazingjobsapp as part of a CRON job.
I just can't seem to find any examples on how to do this. Any pointers are greatly appreciated.
EDIT:
I've used to code below and now am getting:
object(FacebookApiException)#11 (8) { ["result":protected]=> array(1) { ["error"]=> array(3) { ["message"]=> string(72) "(#200) The user hasn't authorized the application to perform this action" ["type"]=> string(14) "OAuthException" ["code"]=> int(200) } } ["message":protected]=> string(72) "(#200) The user hasn't authorized the application to perform this action" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(86)
Can someone walk me through granting my app manage_page and publish_stream permissions for page id 413282938687554 please?
EDIT 2: If I look at my graph.facebook.com/me/accounts then it shows the page id above with an access token but if I use that in my code then I still get the error.
You need to get the manages_pages permission
<?php
$attachment = array(
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://mylink.com',
'description' => 'this is a description',
'picture' => 'http://mysite.com/pic.gif',
'actions' => array(
array(
'name' => 'Get Search',
'link' => 'http://www.google.com'
)
)
);
// To authenticated user's wall
$result = $facebook->api('/me/feed/', 'post', $attachment);
// To page's wall
$result = $facebook->api('page-id/feed/','post',$attachment);
// To user's wall
$result = $facebook->api('user-id/feed/','post',$attachment);
Ok maybe I missed something in the docs but I found the solution with is as follows:
Note this is for people who are trying to post from a cron job.
Grant the manage_pages, publish_stream and offline_access perms to
your app
Get the access token for your page from me/accounts in the graph api explorer
Add one simple line of code just after you initialize your fb object
$facebook->setAccessToken("XXXXX");
Note the access token needs to be the PAGE access token you got from step 2 and remember if you de-auth the app the access token will change.
Application will need to ask you for manage_pages & publish_stream permissions.
Obtain your pages page_access_token from the graph api /accounts call. Each page you own will have its own token listed. This token can be saved to database or accessed at the time of need. It is not recommended to expose or share this token.
Use "php-sdk" to make the post.
refer to: http://developers.facebook.com/docs/authentication/
refer to: http://developers.facebook.com/docs/reference/php/
NOTE: To impersonate the Page when posting to the wall (i.e. post as the Page, and not the current user), you must use a Page access_token with the manage_pages and publish_stream permissions, as described under Page Access Tokens above.
Example: Assumes user has installed and initialized php sdk for Facebook
<?php
if($user){
$vars = array(
'message' => "message goes here",
// you can uncomment below to enable an image with caption and link.
//'picture' => "image",
//'link' => "Link here",
//'name' => "Name here",
//'caption' => "Caption here",
'show_error' => true,
// comment out the access_token array below to post as a user instead of as page.
array('access_token' => YourPageAccessToken)
);
$testpost = $facebook->api('YourPageId/feed', 'post', $vars);
}
?>
If you find this answer helpful or is correct please remember to mark
it up or mark it as correct, so future users will have an easier time
finding answers.
Ok where I had thought we'd fixed it we haven't the post only works when i'm actually logged into the app itself.
I'm using Shawn's code with all the permissions mentioned with the access token from "me/accounts/" for the AmazingJobs Community page.
I've definitely granted manage_pages, publish stream and offline_acess to my own facebook account i then used the graph api explorer to find the access token within accounts and cut and pasted it into my code. Any ideas?

Facebook timeline wallpost doesn't work as aspected

Im using the Facebook SDK Api [PHP] to post automaticly on a timeline wall.
I created a App and requested the access_token with the following permissions: manage_pages,publish_stream,offline_access.
Im using now the access_token to post the message on a page (/PAGEID/feed) where im moderator.
The following is going OKAY and get the post working under the name of the Facebook page:
$attachment = array(
'access_token' => $accessToken,
'message' => $description,
);
$res = $facebook->api('/PAGEID/feed', 'POST', $attachment);
When Im adding some link + name AND/OR picture it will post not as wall post but as "recent message of others" with as name my own user:
$attachment = array(
'access_token' => $accessToken,
'message' => $description,
'link' => 'http://www.google.nl',
'name' => $description,
'picture' => $image,
);
How can I post full messages with link and picture on the wall self?
If I understand you correctly, you are trying to post as the Page but it's showing up as a post by you. Is that correct?
If so, I think you need to post using the Page's access token, rather than your own access token. Look at the section called "Page Access Tokens" on https://developers.facebook.com/docs/reference/api/page/. Once you get the page's access token using the technique described there, you can set it to be the token that PHP SDK uses by doing $facebook->setAccessToken(.... access token value ...). Then you can do the post and it should show up as having been created by the page.
This problem troubled me for so long. Everything seemed setup correctly but as soon as I added a link, it would post as Admin instead of Page. The answer above by triangle_man solved my problem. When using the PHP sdk, the specific line of code I needed was:
$page_token = $facebook->api("/$page_id?fields=access_token");

Posting on User's friend's wall with Facebook

Is there any way to post something to a user's friend's wall from that user in my facebook app?
Example: A user of my facebook app answers a question about one of his friends. I want a small message to be posted to the wall of that friend saying 'Bob answered a question about you...'
Is that possible? If so, how?
It seems that this is possible, the following code is working:
$result = $facebook->api('/$friendsUserId/feed', 'POST', array(
'name' => 'TEST NAME',
'caption' => 'Test Caption',
'description' => 'Test Description',
'message' => 'this is a test.',
));
print_r($result);
die;

Posting to a friends wall on facebook privately using graph api

I wanted to post a feed to a friends wall from my app and set it as private, which is viewable to the logged in user and the friend.
I want to do this using the new Graph api, I saw that if I set the "to" parameter in the feed post, it will be posted to the users friend wall.
I found a code here:
http://forum.developers.facebook.net/viewtopic.php?id=56458
(Posted by VovaOnline)
(link is dead, as facebook has taken down the forum)
$result = $facebook->api('/me/feed', 'POST', array(
'from' => array(
'name' => 'Vladimir Ageenko',
'id' => '100001308281502'
),
'name' => 'TEST NAME',
'caption' => 'Test Caption',
'description' => 'Test Description',
'message' => 'This is test.',
'privacy' => array(
'description' => 'Vladimir Sergeevich',
'value' => 'CUSTOM',
'friends' => 'SOME_FRIENDS',
'allow' => '100001338940933'
)
));
I am setting the post type as "link".
Can anyone tell me what is wrong in this code. One thing I know that "from" field has to be "to" and it has to be passed in a "data" variable. I am not sure how to do it.
Can any one help me?
You must encode privacy array, try this:
$privacy = array(
'description' => 'Vladimir Sergeevich',
'value' => 'CUSTOM',
'friends' => 'SOME_FRIENDS',
'allow' => '100001338940933'
);
$result = $facebook->api('/me/feed', 'POST', array(
'from' => array(
'name' => 'Vladimir Ageenko',
'id' => '100001308281502'
),
'name' => 'TEST NAME',
'caption' => 'Test Caption',
'description' => 'Test Description',
'message' => 'This is test.',
'privacy' => json_encode($privacy)
));
I don't think Facebook allows you to do this currently. Look at the graph API docs on Posting:
http://developers.facebook.com/docs/reference/api/post/
it says on privacy:
"Note: This privacy setting only applies to posts to the current or specified user's own Wall. Facebook ignores this setting for targeted Wall posts (when the user is writing on the Wall of a friend, Page, event, group connected to the user). Consistent with behavior on Facebook, all targeted posts are viewable by anyone who can see the target's Wall. "
I interpret this to mean that if you post on someone else's wall (feed), privacy is out of your control.
From now on it's not possible to post on user's friend wall due to February 6, 2013 breaking changes:
https://developers.facebook.com/roadmap/completed-changes/
Removing ability to post to friends walls via Graph API We will
remove the ability to post to a user's friends' walls via the Graph
API. Specifically, posts against [user_id]/feed where [user_id] is
different from the session user, or stream.publish calls where the
target_id user is different from the session user, will fail. If you
want to allow people to post to their friends' timelines, invoke the
feed dialog. Stories that include friends via user mentions tagging or
action tagging will show up on the friend’s timeline (assuming the
friend approves the tag). For more info, see this blog post.