I read all threads on this forum but I can't find my solution..
I use the last Php SDK by facebook : v3.2.2 .
So I used this:
//start fb
$facebook = new \Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_app_secret,
'cookie' => true,
));
// get id user-fb
$fbUser = $facebook->getUser();
if ($fbUser) {
try {
$fbUserProfile = $facebook->api('/me'); // take user-infos
$friends = $facebook->api('/me/friends'); // take his friend
} catch (FacebookApiException $e) {
error_log($e);
$fbUser = null;
}
}
if ($fbUser) {
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => ('my-url') ));
} else {
$loginUrl = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'email,offline_access,publish_actions',
'req_perms' => 'publish_stream,email',
'redirect_uri' => 'my-redirect-url'
));
}
So I don't understand what is wrong..
When I test it, first time I can click on the loginUrl and get authorization . but when I try to getUser again, the function $facebook->getUser() return 0 again and I got the loginUrl again but when I click on the link, I'm just redirect on the callback url..
I'm not sure if the application facebook need special parameters for this. My app_id and app_secret are 100% sure.
I solved this issue using $facebook->getLoginUrl(array(
'scope' => 'public_profile',
'redirect_uri' => 'http://www.your_domain/current_page.php'
));
where 'http://www.your_domain/current_page.php' is the same page where i am running this code
N.B using Facebook php sdk 3.2.3
(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
OK I solved the error: the callback url was wrong, I needed to call the current url and not a special one.
Related
Hey all i am using the following code to post to a posting on my news feed:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'fileUpload' => true,
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try {
$access_token = $facebook->getAccessToken();
$user_profile = $facebook->api('/me');
$comment = $facebook ->api('/xxxxxxxxxxxxxx/comments',
'POST',
array(
'access_token' => $access_token,
'message' => 'testing!'
)
);
} catch (FacebookApiException $e) {
echo ($e);
$user = null;
}
}
<?php if ($user): ?>
Logout
<?php else: ?>
Login with Facebook
<?php endif ?>
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$params = array(
'scope' => 'read_stream, friends_likes, email, read_mailbox, read_requests, user_online_presence, friends_online_presence, manage_notifications, publish_actions, publish_stream, user_likes, user_photos, user_status, user_videos, read_insights'
);
$loginUrl = $facebook->getLoginUrl($params);
}
?>
<?php print_r($user_profile); ?>
For some reason i get this error:
OAuthException: (#221) Photo not visible
And i have no idea since i am posting a text comment and not even an image??
If i comment out the code line $comment = $facebook ->api('/xxxxxxxxxxxxxx/comments',
'POST',
array(
'access_token' => $access_token,
'message' => 'testing!'
)
); it works just fine (as in, displays my info with user_profile). I've tried reading over the page that tells you how to use the comments here and i do - it just doesn't seem to want to work?
What am i missing???
update
using the graph API i was able to do the same thing i am trying to do via PHP so i know it works...:
I know it's an old question but...
First, for tests the same enviroment, you have to use your app token, not the Graph API Explorer. Otherwise, you're testing the API with all the permissions.
Second, your problem must be a permission thing (that's why it's working on the API Explorer). You should:
Ask for publish_permission on the login (not recommended) or,
Ask for publish_permission when the user is ready to send the comment.
More information about this: Optimizing Permissions Requests
Here the permissions that you need: Publishing Comments
Permissions
As a general rule, when you see a OAuthException, you are having a permission issue.
If the post is photo type, you're going to need a user_photos permission.
Whenever I try to login a prompt opens asking for the basic permissions, after that its being redirected to my redirect_uri with an URL
=">http://localhost/demo/?code=AQDwzia3Wx1BktixF59jVHbm0ViGVJm8Xhb2tNZDyYreZh0KoSJhrSsJ8Aa2KX3gocwR0XNQjQz7ZlBh26_nBi-3iOMByhVO2cxwJ8maC4IHxBacfqXjzqIyBaZQbWKUUxPI6VBrqBgFXQasj7PEtmug7lt93dK4fmMC2A4i2dUYU-gSvzn0f0ZdB3eT_aSvgR1KoLCmQgLh3xix4H05QR6LCP9nLtQC4l9rMJW83kS0PNmWq0COZYvGfuX1R7519Fn3iXRB9F0MTsK1KQ_ulpK84PUCkuMu8et88Lln0ZwuzaPo0oERelkPWYnrrTKa-5w&state=ed66ea618d8076d9e72c15d9a65a6312#=
Even though facebook->getUser() returns 0
Here is my code
<?php
require_once('php-sdk/facebook.php');
$facebook = new Facebook (array(
'appId' => '1234',
'secret' => '12313',
'cookie' => true
));
?>
<html>
<head> <title> Warming Up with FB API </title> </head>
<body> <h1> Hello World </h1> </body>
</html>
<?php
$loginUrl = $facebook->getLoginUrl(array (
'display' => 'popup',
'redirect_uri' => 'http://localhost/demo'
));
$user = $facebook->getUser();
//echo $user. '</br>';
if ($user) {
echo '<em>User Id: </em> '.$user;
} else {
$loginUrl = $facebook->getLoginUrl(array (
'display' => 'popup',
'redirect_uri' => 'http://localhost/demo'
));
echo 'Login Here ';
}
?>
I know its a very trivial question but I am kinda stuck at this and unable to proceed further. Kindly suggest what to do.
*UPDATE*
Leaving the App Domain empty solved my problem.
if someone is still banging their head over this here is what is wrong now. I was hired to fix this mess!
Check App domain in fb panel , must match with the domain where your app is.
edit base_facebook.php find:
public function getAccessToken() {
if ($this->accessToken !== null) {
// we've done this already and cached it. Just return.
return $this->accessToken;
}
// first establish access token to be the application
// access token, in case we navigate to the /oauth/access_token
// endpoint, where SOME access token is required.
$getApplicationAccessToken = $this->getApplicationAccessToken();
$this->setAccessToken($getApplicationAccessToken);
$user_access_token = $this->getUserAccessToken();
if ($user_access_token) {
$this->setAccessToken($user_access_token);
}
return $this->accessToken;
}
to::
public function getAccessToken() {
if ($this->accessToken !== null) {
// we've done this already and cached it. Just return.
return $this->accessToken;
}
// first establish access token to be the application
// access token, in case we navigate to the /oauth/access_token
// endpoint, where SOME access token is required.
$getApplicationAccessToken = $this->getApplicationAccessToken();
$this->setAccessToken($getApplicationAccessToken);
$user_access_token = $this->getUserAccessToken();
if ($user_access_token) {
//$this->setAccessToken($user_access_token);
$this->accessToken = $user_access_token; //edit; msolution
}
return $this->accessToken;
}
Next find the function: getAccessTokenFromCode()
find the line:
parse_str($access_token_response, $response_params);
replace it with:
//parse_str($access_token_response, $response_params); //edit:: msolution;;
$response_params = json_decode($access_token_response, true );
commenting the original and adding json_decode
thats it!
If User is logged in and still getting $user = 0
I faced this issue on different occasions but SDK issue is not happening to everyone. So I'm not sure what goes wrong here. But I dig in to it little bit and found solution as mentioned below.
SOLUTION : This worked for me after trying for many solutions for this issue.
In base_facebook.php file, find the makeRequest() method and check for following Line.
$opts = self::$CURL_OPTS;
Immediately following it, add this line
$opts[CURLOPT_SSL_VERIFYPEER] = false;
More details can be found here - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/
Change this
$loginUrl = $facebook->getLoginUrl(array (
'display' => 'popup',
'redirect_uri' => 'http://localhost/demo'
));
to
$loginUrl = $facebook->getLoginUrl(array (
'display' => 'popup',
'redirect_uri' => 'http://localhost/demo/index.php'
));
And see if this works !!
After several hours, the solution for me was (Let's say my site is http://www.site123.com):
Settings # https://developers.facebook.com
App Domain: site123.com (even blank it will work though)
Site URL: http://site123.com IMPORTANT : NOT http://www.site123.com
PHP Code - fb_config.php
//Declarations
$app_id = "{you AppID}";
$app_secret = "{your App Secret}";
$site_url = "http://site123.com/receive_data_from_facebook.php";
//New Facebook
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => TRUE, /* Optional */
'oath' => TRUE /* Optional */
));
//Login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,user_birthday',
'redirect_uri' => $site_url,
));
//Get FacebookUserID
$fbuser = $facebook->getUser();
Important Note Also: I had some issues with Firefox $facebook->getUser() was 0 even my code was working on Chrome. But after cleaning Firefox cache, it worked nicely!
Hope it helps.
It might be Worked using
$opts[CURLOPT_SSL_VERIFYPEER] = false; after $opts = self::$CURL_OPTS; on file base_facebook.php.
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 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.
So I've setup the basic example code the SDK suggests.
I'm having such a problem, well many problems as I'm sure you all are too.
I've used both the session approach with SDK 2.12, my current problem is, even though I'm passing the getLoginUrl a redirect_uri, it ignores this and redirects it to my base_domain +
'{"session_key":"badsdsadsdsdasdsd.0-1268121831","uid":"badsdsadsdsdasdsd","expires":0,"secret":"730aa9badsdsadsdsdasdsd","base_domain":"slumber-roo.co.uk","access_token":"badsdsadsdsdasdsdEZBZAZAvdRAZC6PwPeBZBYMZD","sig":"badsdsadsdsdasdsdd761385"}'
Obviously filled in with keyboard smash.
Now, my attempt with SDK 3.1 is weird. I have a valid getUser, but my colleague doesn't and gets stuck in the infinite loop.
I do understand the differences between getSession and getUser with SDK 2 and 3 respectively.
Here is my code:
<?php
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
//Facebook Authentication part
$session = $facebook->getSession();
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
print_r($me);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,publish_stream,user_hometown',
'redirect_uri' => 'http://www.facebook.com/pages/CharlesTestPage/225802194155435?sk=app_252946408094785'
)
);
exit('<script> top.location.href="'.$loginUrl.'"; </script>');
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
Oh, in addition, I'm running this app solely inside a tab.
Thanks in advance.
Is the application in sandbox mode? Have you added your collegue as an ADMIN || DEVELOPER || TESTER in the app config?
I just got that this morning, my project manager was in an infinite loop, added her as tester in the app but she hadn't accepted the pending invite and was looping.