Is it possible to post to a friends wall/timeline in facebook using an app even if the user is not currently logged in? Here’s the code that I’m currently working on, as you can see it posts a message on a friends wall(substitute the friend_id with an actual profile id). This works but a user has to logged in in order to perform this operation.
I don’t have any idea what to do if I want this script to execute automatically(without user intervention).
<?php
require 'php_sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
$session = $facebook->getUser();
$me = null;
if($session){
try{
$me = $facebook->api('/me');
$facebook->api('/friend_id/feed', 'post', array('message'=>'hello without user!'));
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}
if($me){
$logoutUrl = $facebook->getLogoutUrl();
echo "<a href='$logoutUrl'>Logout</a>";
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_about_me,user_birthday,user_location,email,read_friendlists,friends_location,friends_birthday,publish_stream'
));
echo "<a href='$loginUrl'>Login</a>";
}
?>
Any ideas? Code samples, and links to specific documents that can help me gain a bit of idea on how this works is greatly appreciated thanks!
When the user is logged in, you need to trap their “Access Token”
To post as them later, you use the access token
The token will be valid for 2 days. You can extend this to 60 days if you want.
Edit: For exenteding the token, you call the function
immediately before “getAccessToken” call.