Facebook API - post/share activity from my app to users wall - facebook

I'm creating an facebook app for kart racing. I've users registrated with Facebook, with publish_actions scope.
This gives me an opportunity to post to his wall his results, until the access token expires (2 hours).
My clients would need to post a message or share a link or share an activity everytime the user finishes the race.
The code I use now is:
$config = array(
'appId' => 'xxxx',
'secret' => 'xxxxx',
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
}
Is there other permission I need to get to be able to post somehow to users wall, or share link? How can this be achieved?
Edit:
Just to be more specific, the kart racing isn't a facebook online game, it's real world kart racing, and I would like to post the racer result to his wall somehow after the end of the race.

Related

how to post facebook user wall/ page status by a single user only no app install for other users

I am working on a site and i need to post page links from that site to a special user wall or page if something is published on it which means i only need one user to post that question.
the problem which i am facing is access token since i am not trying to show facebook login page in front of website traffic. its not like sharing on user wall we are basically trying to post status on a page automatically so any help for this problem will be appreciated.
i am using the following code which seems to work fine as long as the access token for the page is valid but i need to make something permanent so that i can add all the info in the php code hardcoded and it work for some weeks at-least without changing the api key or token
$appid = 'code';
$appsecret = 'code';
$wall= $pageId = 'code';
$token='page token';
$facebook = new Facebook(array('appId' => $appid, 'secret' => $appsecret, 'cookie' => true));
$params=array( 'message' => $msg,'name' => $title, 'caption' => $title, 'link' =>$uri,'description' => $desc, 'picture' => $pic, 'access_token' => $token,'actions' => json_encode(array('name' => $action_name,'link' => $action_link)) );
$result = $facebook->api($wall.'/feed/','post',$params);
if i make token from the graph api debug page then it works fine but after some time i got the message "OAuthException: An active access token must be used to query information about the current user."
i am really desperate to find something solid for it.
thank you
You can write a backend script (maybe a cron job using the FB PHP-SDK) that runs as the special user and makes the desired FB api calls. For this, you will want to use a long-lived access token, which needs to be renewed every 60 days. You might also try your FB api calls using an App Access Token, which do not expire but also do not support all FB publishing and other operations.
To post to someones wall (or perform any action with the open graph) you must be authenticated with facebook as somebody in their graph or an application that they have granted permission to.
You should look at the permissions that can be set using the oauth scope attribute...
https://developers.facebook.com/docs/reference/dialogs/oauth/
To me it looks like they will need to grant your application publish_stream if you want to write to their friends walls as well as the user that you are authenticated as.
https://developers.facebook.com/docs/reference/login/extended-permissions/

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?

how to tell if the user is already logged in and has already accepted my app

I am just creating a facebook tab on a users page.
I am using the php facebook SDK, and this little snippet(see below) from the tutorials.
My question is , if the user is logged into FB and comes back to my page later $user is not found and they have to connect again. Is there not a way to know that they are already logged in and already granted me access?
I hope this question makes sense
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
'cookies' => 'true'
));
$user = $facebook->getUser();
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'publish_stream, manage_pages'));
}
Nope, it won't be done automagically on server-side. To know who user is - you need to ask them to follow the login url
OR
use JS SDK. In that case user is logged in automatically and all you need is to refresh the page by JS (there is an example shipped with php sdk on how to do that)

Creating facebook apprequests

I'm trying to send a message from an app to a user when a specific event happens. Right now I have this code
$param = array(
'message' => 'XYZ shared a file with you',
'data' => 'additiona_string_data',
'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/$uid/apprequests", "POST", $param);
but I always get Uncaught OAuthException: (#2) Failed to create any app request thrown
I don't know where is the problem.
You should read the requests documentation.
In there is an explination about two different types of requests.
user initiated (with the request dialog )
app generated (with the Graph API)
What you need is the app generated requests, that means you'll need the apps access token and not the users.
I assume that you are using the users access token because you did not include the initiation of the facebook object in your code sample and have probably verified the user already hence the getAccessToken() call will return the users access token and not the applications access token.
I'm a little confused as to "I'm trying to send a message from an app to a user when a specific event happens. Right now I have this code" means.
Sending an email to a user when someone posts on their wall
Sending an event invite to a user
Sending an app invite to a user
Writing on a users wall when something happens like 'XYZ shared a file with you'.
To answer
You need email and read_stream permissions of the user. Monitor his wall using the RealTime Updates and then email him using your server SMTP.
See http://developers.facebook.com/docs/reference/api/event/#invited on how to create an event invite
As #Lix pointed out, see https://developers.facebook.com/docs/channels/#requests
You should accomplish this using the new Open Graph object/actions. See this example: https://developers.facebook.com/docs/beta/opengraph/tutorial/
You can receive Facebook app access token via:
https://graph.facebook.com/oauth/access_token?client_id=FB_APP_ID&client_secret=FB_APP_SECRET&grant_type=client_credentials
Working code sample to post app-to-user request using Facebook PHP SDK (add error handling where required):
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
));
$token_url = "https://graph.facebook.com/oauth/access_token?" ."client_id=" .
FB_APP_ID ."&client_secret=" . FB_APP_SECRET ."&grant_type=client_credentials";
$result = file_get_contents($token_url);
$splt = explode('=', $result);
$app_access_token =$splt[1];
$facebook->setAccessToken($app_access_token);
$args = array(
'message' => 'MESSAGE_TEXT',
);
$result = $facebook->api('/USER_ID/apprequests','POST', $args);