I'm trying to add a facebook login to my site but the problem is that the facebook function "getUser" only works sometimes. Half the time I'll get the user's facebook id from it, but half the times I won't.
Is there a delay somewhere? How can I fix this?
My if facebook_uid set fails and it all ends up in a big mess. :(
Facebooks getSession never fails though. All the above happens when a facebook session is correctly initiated.
Please help!
Here's part of my code:
$facebook = new Facebook(array('appId' => '123','secret' => '789','cookie' => true,));
$facebook_session = $facebook->getSession();
if ($facebook_session) $_SESSION['facebook_uid'] = $facebook->getUser();
Related
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/
I am trying to implement the Facebook Login and Logout in my website.
I am using the Facebook PHP SDK.
The code i am using is as follows :
Login
$facebook = new Facebook(array('appId' => APP_ID,
'secret' => APP_SECRET
));
$param = array();
$param["scope"] = array("email","offline_access","publish_stream");
$loginUrl = $facebook->getLoginUrl($param);
Logout
$logoutUrl = $facebook->getLogoutUrl();
The problem is logout url is not being able to logout the facebook user.
When i remove the "offline_access" from the scope parameters the logout Url is working fine.
I have also implemented the above scope in the example.php file in the PHP SDK and the result was the same.
Can anyone provide any help.
I found out my problem. After logging in from Facebook I called the function $facebook->destroySession(); and after that, called the function $facebook->getLogoutUrl();.
Because of the destroySession() function the access_token returned from Facebook got lost and my logout URL generated from the getLogoutUrl() function could not log out the user from Facebook.
After removing the destroySession() function my code is working fine.
I think the problem might be that you are not clearing the session cookie after you log the user out of FB.
1) download the latest php sdk.
2) Make sure you specify the 'domain' parameter when you create the $facebook object.
3) Before you redirect the user to FB logout, clear their session with
$facebook->setSession(null); - alternatively, you can use FB.logout() in the javascript SDK.
Update:
as you are specifying appId and key, also give 'domain' as your domain name.
Examples of setSession:
example1
I'm trying to redirect users to the facebook login page then back to my app without the Log In to app page displaying. Is this possible?
I've looked into the "next" parameter which is automatically generated by the PHP-SDK but cannot seem to change it.
Is there anyway to do what I'm looking for? I want to make sure users are logged in in order to check to see if they have authed my app but do not want them to if they are not already.
Thanks for any help!
No, i don't think it is possible to modify the next parameter. The only place where i saw the next parameter(in the PHP SDK) is in the getLogoutUrl() function, as seen in the source of the base_facebook.php. And that parameter defines which url to go, after logout.
For getLoginUrl() in the base_facebook.php file, we have
return $this->getUrl(
'www',
'dialog/oauth',
array_merge(array(
'client_id' => $this->getAppId(),
'redirect_uri' => $currentUrl, // possibly overwritten
'state' => $this->state),
$params));
as the last line in the function, which obviously means that the oauth-dialog will be shown, no matter what you try.
If your app is not on Facebook, then you can go for <fb:login-button>Login with Facebook</fb:login-button> in Javascript, and subsequently follow the example from this url. You'll see there that it's not possible to remove that dialog entirely, even if you don't ask for any permissions.
The Javascript SDK's FB.login() method will also bring up the auth dialog.
Hope this helps.
They have to accept permissions for the first time only. Facebook will never let you to get user's data without their knowledge/accept
If you are using php :
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
//Facebook Authentication part
$user = $facebook->getUser();
if (!$user) {
// user is not logged on --> redirect to the login url
} else {
// user is logged on you can do what you want
}
We redirect users to below URL on mobile phones for application authorisation:
https://m.facebook.com/dialog/oauth?client_id=XXXXXX&redirect_uri=http://www.server.com/callback.php&scope=offline_access,user_likes,publish_stream,publish_checkins,user_checkins&display=wap
If the user is logged in to Facebook on his/her phone, no problem, Facebook automatically redirects to oauth dialog page.
If user is not logged in, Facebook asks them to login first.
On wap site(A Nokia phone), it redirects to oauth dialog without any problem after login.
But on touch site(An iPhone), it add hastags to URL, redirects user to his/her Facebook homepage.
Even display=wap parameter on URL doesn't help on this issue.
Any ideas on how to solve this problem?
Thank you
Actually, here's a cleaner solution. (I hadn't seen the API for getLoginUrl at the time of my previous post. http://developers.facebook.com/docs/reference/php/facebook-getLoginUrl)
require_once("facebook.php");
$config = array(
"appId" => APP_ID,
"secret" => APP_SECRET
);
$facebook = new Facebook($config);
$params = array(
"scope" => "offline_access,user_likes,publish_stream,publish_checkins,user_checkins",
"redirect_uri" => "http://www.server.com/callback.php",
"display" => "touch"
);
$url = $facebook->getLoginUrl($params);
header("Location: $url");
I experienced the same problem and had troubles isolating it since it worked in Chrome on the desktop but not while using the iPhone's Safari browser.
I did a urlencode around the redirect_url parameter and set display to touch. So to use your link above as an example, I'd try this:
https://m.facebook.com/dialog/oauth?client_id=XXXXXX&redirect_uri=http%3A%2F%2Fwww.server.com%2Fcallback.php&scope=offline_access,user_likes,publish_stream,publish_checkins,user_checkins&display=touch
I sincerely hope that works for you. It seemed to have brought me luck.
I was developing facebook canvas application, and I found this simple code fails. I don't know what went wrong, because I took it straight from tutorial:
<?php
require 'facebook.php';
/*
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
*/
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
));
//Request params
if(!($facebook->getSession()))
{
header("Location:" . $facebook->getLoginUrl(array('req_perms' => 'publish_stream')));
exit;
}
?>
the problem is at header("Location:" . $facebook->getLoginUrl(array('req_perms' => 'publish_stream')));. When I remove it (including removing the exit), the application can run well. However, when I have it, the application doesn;t show anything. Just blank page. And there is "load resource error from channel.facebook.com" on chrome's developer tools.
Can anybody help me spot what when wrong? I don't understand what went wrong In this code. I have made sure that appId and secret are correct.
This might help you:
load resource error from channel.facebook.com is irrelevant to your problem, I've seen that error plenty of times on the facebook page, it is their problem.
Most likely you are getting a blank screen because there is a PHP error, and you have error_reporting set to 0. Try error_reporting(E_ALL) to see if this is the problem.
Where is this code located, is the header you are sending ahead of other text output? Try placing the code on top of the page.
These are just some basic stuff you can check, hope it helps. Good luck.
I had the same problem (blank page) when trying to redirect (in PHP from the server side) to the login URL generated with the function getLoginUrl of the Facebook PHP SDK.
The problem seems to be related to the fact that the redirection is in the iFrame.
The redirection needs to be for the parent frame and not the canvas frame.
In PHP there is no way to tell the browser to redirect the parent frame soo you have to do it in Javascript:
$loginUrl = $facebook->getLoginUrl(array('req_perms' => 'publish_stream'));
echo '<script type="text/javascript">top.window.location="'.$loginUrl.'";</script>';