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"
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
// NOTE: wrap these in try/catch blocks for safety - this is example only
$session = $facebook->getUser();
$access_token = $facebook->getAccessToken();
To post as them later, you use the access token
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
// NOTE: wrap these in try/catch blocks for safety - this is example only
$facebook->setAccessToken($access_token);
// Then check /me and you should still have the same user.
// Then post to /me/feed to post to the wall.
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
$facebook->setExtendedAccessToken();
immediately before "getAccessToken" call.
Related
I am getting "(#200) The user hasn't authorized the application to perform this action" error when posting message on user's facebook wall. I am using facebook graph API. I have also set the "publish_actions" permission in the scope, but didn't working. My code is
require 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
$accessToken = $facebook->getAccessToken();
try {
//create message with token gained before
$post = array(
'access_token' => $accessToken,
'message' => 'Test message'
);
$res = $facebook->api('/me/feed', 'POST', $post);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_actions'
));
header("Location: " . $login_url);
}
Can you please suggest me what i am missing in the code or in the app?
Since you mentioned that it does work for Admins, but not for anyone else, it´s very clear what the problem is: You need to go through Login Review with publish_actions - else, that permission will only work for users with a role in the App.
All the information you need about Login Review is in the docs: https://developers.facebook.com/docs/facebook-login/review
I am using php sdk for facebook login on my website. But i am facing a very stupid problem, when i click on connect facebook button it redirects me to facebook website to enter email and password but after redirecting back to my website it creates infinite loop which does not end.
This is my code:
require('facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
//echo $user;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
print"<pre>";
print_r($user_profile);
print"</pre>";
You can test the it on this url:
http://www.conceptbeans.co.uk/cb/projects/xedact/login.php
I will be appreciable any help from your side.
Thanks
Zain
your syntax isnt correct when you define your new facebook object, you have a , at the end of array. should be:
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret'
));
Every time I run this script it gives me a different access token. I thought the access token was unique to a user.. Also why is this script so unreliable, fails half the time.
My Facebook Access Token Page
Here is the code:
<?php
require 'lib/facebook.php';
require_once('C:\inetpub\storeboard.com\linkedin\extract_data.php');
$facebook = new Facebook(array(
'appId' => FACEBOOK_APPID,
'secret' => FACEBOOK_APP_SECRET
));
$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) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
$access_token = $facebook->getAccessToken();
if (isset($access_token)) {
echo $access_token;
//header( 'Location: http://www.'.APP_ROOT_DOMAIN.'/facebook/save_token.asp?nToken='.$access_token);
}
?>
Have I cut out code that is required from the original: http://developers.facebook.com/blog/post/534/
or am I missing something.
require_once('C:\inetpub\storeboard.com\linkedin\extract_data.php');
This line of code just extracts the APPID and APPSECRET from the database.
I am a complete beginner when it comes to PHP, so any help would be greatly appreciated.
Many Thanks!
You don't need to play with the token to get simple authentication:
After you get facebook api instance:
$facebook = new Facebook(array(
'appId' => FACEBOOK_APPID,
'secret' => FACEBOOK_APP_SECRET
));
If you are using me API - get the user profile:
$user_profile = $facebook->api('/me');
then obtain email from it:
$email = $user_profile['email'];
At this point you check if the email is valid in your database - if yes start the user session and you are good to go.
I am using a Facebook login in one of my website using the sdk for PHP.
Sometimes the facebook login works and sometimes it doesn't.
Here is my index.php:
/// fb login area.....
include("facebook/src/facebook.php");
$facebook = new Facebook(array(
'appId' => '159326560901256',
'secret' => 'c6b30bd2f80747b64d5b1d15a3da9e9c',
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => 'http://xyz.com/login_fb.php'
));
<a href="<?=$loginUrl?>"><img class="fb_img"
src="images/fb_logo.png" width="153" height="38" /></a>
it goes to login_fb.php:
https://www.facebook.com/dialog/oauth?client_id=123131231&redirect_uri=http%3A%2F%2Fxyz.com%2Flogin_fb.php&state=403bc8aaf9eaee4064e3cc27befc71ff&scope=email
now on login_fb.php i get this url:
login_fb.php?code=ASA&S^&*A^S&*A^S&*^A&*ASUAUIYSUIYASIYAUSYUIASA
and in $user I get 0
/// code for login_fb.php
include("facebook/src/facebook.php");
$facebook = new Facebook(array(
'appId' => '159326560901256',
'secret' => 'c6b30bd2f80747b64d5b1d15a3da9e9c',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
$logoutUrl = $facebook->getLogoutUrl(array(
'next' => 'http://minuteville.com/index.php?val=fblogout'
));
Why isn't it working?
By adding the following line and using getAccessTokenFromCode in Facebook SDK,
the problem can be resolved.
The acesss code expires. So getting new access code by exchanging code resolves the problem
$access_code = $facebook->getAccessTokenFromCode($_GET['code']);
https://developers.facebook.com/docs/howtos/login/server-side-login/#step6
You should be able to build the access token with the code parameter to get the user. It's all written in the doc.
I'm writing a Facebook application which fetches some info about the user and does some manipulations on it.
I get the user permissions like this:
$facebook = new Facebook(array(
'appId' => APPID,
'secret' => 'APPSECRET',
));
$logoutUrl = $facebook->getLogoutUrl();
(The default permission is for user_info)
$user_profile = $facebook->api('/me');
After the user logs in, the application goes through several PHP pages, and in the shows a msg.
In the last PHP page, I try to get once again the user info.
$facebook = new Facebook(array(
'appId' => APPID,
'secret' => 'APPSECRET',
));
$logoutUrl = $facebook->getLogoutUrl();
$user_profile = $facebook->api('/me');
My problem is that on some browsers (Chrome) it works, and on other browsers (IE) it doesn't.
I get an Oauth exception that I don't have a valid authentication key.
Can you see why is that?
The thing is very simple:
First you init facebook:
$facebook = new Facebook(array(
'appId' => APPID,
'secret' => 'APPSECRET',
));
Then you get the user
$user = $facebook->getUser();
And finally you test if there a user or not, if there you do all the request to the facebook api and generate the logout url, if not you get the login url and redirect the browser to that url.
if ($user){
$user_profile = $facebook->api('/me');
$logoutUrl = $this->facebook->getLogoutUrl();
else{
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email,user_birthday'));
echo '<script type="text/javascript">top.location.href = "'.$loginUrl.'"</script>';
}
This should work for all browsers because I tested, so good luck.
kick off the authentication flow in such a case.
and if this still happens, see if this is realted to cookies settings.
try to set the header properly.