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.
Related
I keep getting this error when I try to use my app I created for Facebook. When I go the the url for the app it take me to the login page, but when I log in I get a pop up box with the error.
App Not Setup: The developers of this app have not set up this app
properly for Facebook Login.
Every time I click ok it just reloads the popup again.
I have already changes the satatus to Live under status and review in my dashboard.
When I log into my account on facebook, it works fine, but I suppose this is because I am set as an admin for the app on the dashboard.
But other users get the problem above.
The code used:
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
redirect($login_url, 'refresh');
}
This code is in my controller of my codeigniter application which I built the app.
How do I fix this?
UPDATE:
After I added the app as a canvas app this error seemed to go away, however there is still a problem.
I added the Facebook app platform and entered the canvas url, secure canvas url etc.
When I go to the app at the canvas page https://apps.facebook.com/MyApp/ - it work fine as I am a app administrator I guess.
However when an external user goes to the canvas page it just remain blank, but if a user goes to the canvas url http://apps.mydomain.com/MyAPP the app asks for their permissions and works fine as a website.
It just remains blank to users on the canvas page. Why doesn't the canvas page ask for permission?
Help Please.
EDIT:
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
echo("<script> top.location.href='" . $login_url . "'</script>");
}
Why doesn't the canvas page ask for permission?
Because you are redirecting inside of the iframe, but the FB login dialog does not want to be displayed in (i(frames) – for the obvious reason, that that would lead to a lot of phishing, since users are supposed to see that the page they are entering their login credentials too is actually the real FB site.
So you have to redirect within the top window instance, via JavaScript:
top.location.href = '…';
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.
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.