Code using facebook php sdk stopped working suddenly - what happened? - php

I have an old piece of php code calling Facebook, that used to work fine until a week ago. Now it suddenly stopped working. Here are the relevant lines involved:
<?php
require_once __DIR__ . '/facebook-sdk-v562/autoload.php';
// -- skipping here less relevant lines --
// Initialize Facebook Environment:
$default_graph_version = 'v2.10';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => $default_graph_version
]);
// Redirect to phase2 for facebook login:
$helper = $fb->getRedirectLoginHelper();
$fbPermissions = ['manage_pages', 'publish_pages'];
$loginUrl = $helper->getLoginUrl($phase2_url, $fbPermissions);
header('Location: ' . $loginUrl);
Now upon reaching the header line, I get: "Sorry, something went wrong. We're working on getting this fixed as soon as we can." -- I have a feeling (not sure) it has to do with using old versions of things? Has something changed in the recent days (May 2022)?
EDIT:
I've made certain updates in order to match new Graph API version. I used compose to download the new php sdk, I modified the require_once accordingly, and I modified v2.10 to v13.0. I still get the exact same error.
I checked the value of $loginUrl that gives me the error after redirection, and it is (shortened): https://www.facebook.com/v13.0/dialog/oauth?client_id=[***]&state=[***]&response_type=code&sdk=php-sdk-5.7.0&redirect_uri=[my correct url]&scope=manage_pages%2Cpublish_pages

I don't know why, but omitting the "permissions" argument to getLoginUrl altogether -- solved the issue.

Related

Proper Facebook PHP SDK login route

I'm trying to setup a basic login w/Facebook button.
This is basically what I'm copying:
https://developers.facebook.com/docs/php/howto/example_facebook_login
The only differences are I'm trying to setup everything in one file (so I can keep all the social sign-on code in one spot), and I'm hoping to create the link and redirect the user all in one.
When I hit the page generated by the handler, I get a "Sorry this page isn't available" message.
The twitter flow is basically the same (it's complete and functioning without issues). I do save the oauth tokens to a database, which I haven't gotten around to doing yet with the Facebook part yet. It'll save the the save table, I just save the oauth provider or something along with the tokens.
Here's the gist:
switch(strtolower($_REQUEST['method'])) {
case 'twitter':
/*ommited, but it's basically the same flow*/
break;
case 'facebook':
// if we don't have a step, we don't know what to do
if (!isset($_REQUEST['step'])) {
die('Fail: invalid step: (none)');
}
// we'll be using the Facebook PHP SDK
require(FACEBOOK_SDK_PATH);
switch(strtolower($_REQUEST['step'])) {
case 1:
// instantiates a new SDK App object
$fb = new Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => '2.0'
]);
// gets helper
$helper = $fb->getRedirectLoginHelper();
// optional permissions parameter
// we want the users email address
$permissions = ['email'];
// generates the url for the user to authorize access
$loginUrl = $helper->getLoginUrl('http://localhost/social-login.php?method=facebook&step=2', $permissions);
// redirect user
header('Location: ' . $loginUrl);
break;
case 2:
// this should include an access token so we can get information about the user and consider them authenticated
echo '<pre>';
var_dump($_SESSION);
var_dump($_REQUEST);
echo '</pre>';
die();
break;
default:
die('Fail: invalid step: ' . $_REQUEST['step']);
}
break;
default:
die('Fail: invalid method . ' . $_REQUEST['method']);
}
I've poured over the documentation above and just can't figure out what's missing. Could it be a setting on the app side? An issue with the localhost callback?
I matched the default graph version to whatever the app gets set as (2.0). I'm not sure if I can change that or not, but I see a lot of code using 2.2 and 2.5 and I can't figure out if that could be part of my issues either. I would assume not, but figured I'd put a note here.
$fb = new Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => '2.0'
]);
You specified the API version wrong, and therefor the SDK has created the wrong login dialog URL.
You need to include the v:
'default_graph_version' => 'v2.0'

Facebook PHP SDK logout user with endpoint V2.5 not working

I have been trying for hours now to log a user out of Facebook.com using the official php sdk V2.4 (https://github.com/facebook/facebook-php-sdk-v4/) and api endpoint version V2.5.
What I found until now is that I should use
$facebook->destroySession();
However this function is not available if I use
$facebook = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.5',
]);
So instead I used
$helper = $facebook ->getRedirectLoginHelper();
$logoutUrl = $helper->getLogoutUrl($user['facebookAccessToken'], 'www.mypage.com');
This returns
$logoutUrl = https://www.facebook.com/logout.php?next=www.mypage.com&access_token=facebookToken
I checked the token and it is correct. However when I redirect to $logoutUrl then facebook does not logout the user but instead redirects to https://www.facebook.com/home.php while the user is still logged in.
I guess that this is due to the new version V2.5? Is there any way to accomplish this task with the new version?
Thanks a lot in advance!
Ok nevermind, I found out why it didn't work. The problem was that I used xampp on my localhost to debug it. But the redirect link was set to the homepage. After I changed the redirect link to the localhost address it was working.
Hope this helps, if anyone else ever struggles with this.
Cheers

Facebook SDK 5 - reading feeds of a page

I am trying to read the posts of a facebook page, using the actual Facebook SDK 5 for PHP. I have created a facebook app, downloaded and "required" the SDK successfully, but I cannot get the request to run, since it always throws a Invalid OAuth access token signature exception.
The code I have so far:
$fb = new Facebook\Facebook([
'app_id' => $appid,
'app_secret' => $secret,
'default_graph_version' => 'v2.5'
]);
$accesstoken = file_get_contents("https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $appid
. "&client_secret=" . $secret
. "&grant_type=client_credentials" );
//following lines are something I just tried, there have been multiple
//Variantions, none ever worked
$cilent = $fb->getOAuth2Client();
$accessToken = $cilent->getLongLivedAccessToken($accesstoken);
All in all my main issues are:
how to get a valid access token? Since I just want to read public data (feeds of a page), I do not need some special permissons and I am quite confused which on Type of access token I need.
Are there any settings within the facebook app I need to make, or make sure, to get this up and running?
I have googled the web for examples, but I could find examples either using older SDKs (if used at all), or showing how to post data.
I do not have too much experience with that topic and I would appreciate some explanation, or a piece of example.
[UPDATE]
I could manage to get the feeds, some dive through the source was needed.
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.5'
]);
$req = $fb->get(
'/<page-id>/feed?fields=full_picture,message,name,picture,story,posts&limit=20',
$fb->getApp()->getAccessToken());
In case of success everything one is looking for is included in $res. For sake of simplicity I removed the error handling.
Because everything is desinged to read public data, there is no need for any access-token huzzle, however, retrieving user Data, posting or something else, is not possible with this solution.
Cheers!

when attempting to connect to facebook with PHP script endless redirects

first facebook asks me to click okay to authorize the app then chrome gives me this error. and the I have this endless redirect problem. I have read many cases about this but none clear and simple enough to get me over this obstacle.
The webpage at __________&redirect_uri=http%3A%2F%2Fmizu.net46.net%2Ffacebookpost6.php&state=_________________&sdk=php-sdk-3.2.3&req_perms=user_status%2Cpublish_stream%2Cuser_photos%2Coffline_access%2Cmanage_pages#=">https://www.facebook.com/dialog/oauth?client_id=__________&redirect_uri=http%3A%2F%2Fmizu.net46.net%2Ffacebookpost6.php&state=_________________&sdk=php-sdk-3.2.3&req_perms=user_status%2Cpublish_stream%2Cuser_photos%2Coffline_access%2Cmanage_pages#= has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
what I know.
facebook.php is working
the secret and appid are being passed correctly by the script.
I must have configured my app settings on the facebook development site almost correctly
how do I know this?
because when I change those variables in my script or adjust the settings
on facebook development server, I dont even get this far, I dont get asked to click ok or see my facebook profile in chrome.
what I dont know.
How to fix this redirect problem. or complete the authentication process. echo $user always returns 0.
my code looks like this:
require 'src/facebook.php';
$app_id = "xxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' =>
'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
thanks for any help
changing the setting to no when asked" is this a desktop app? " in the advanced section of app configuration on developers.facebook.com seem to resolve this issue.

$facebook->getUser() suddenly stopped working

I've got a login with facebook on my website. This has worked well for some time but yesterday suddenly stopped working. I've tracked the problem to the getUser() method which seems to always returns 0 now.
my code looks like:
<?php
require_once('facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
));
$user = $facebook->getUser();
if ($user) {
try {
$profile = $facebook->api('/me');
$logoutUrl = $facebook->getLogoutUrl(
array(
'next'=>$baseUrl.'/fblogin/fblogin.php?logout'
)
);
$userIsLoggedIn=true;
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
$loginUrl = $facebook->getLoginUrl(
array(
'scope'=>'email,publish_stream',
'redirect_uri'=>$returnAfterLoginUrl
)
);
}
}else{
$loginUrl = $facebook->getLoginUrl(
array(
'scope'=>'email,publish_stream',
'redirect_uri'=>$returnAfterLoginUrl
)
);
}
?>
What I've tried (and could find back in my history)
Update the SDK to the latest version
Solution of Facebook PHP SDK - getUser() suddenly returns 0 (adding 2 $CURL_OPTS)
Solution of suddenly, getUser became to return 0.(PHP 3.1.1 SDK) (adding base_domain to $DROP_QUERY_PARAMS)
Solution of Facebook login is suddenly not working anymore? (increasing curlopt_connecttimeout)
Creating a new app, without luck
I'm using PHP Version 5.3.3
I've been trying to get it working since yesterday afternoon, without any luck :(
Does anyone know what might be the problem and more importantly, what might be the solution?
Thank you
Your port 80 or port 241 may be blocked causing a CurlException 7. To check that just upload the example file in your website. It will show the error.
In my case, I had my beta domain hosted on 000webhost.com. It had recently blocked the port 80 which caused the same error. When I moved my domain to another hosting service, the problem was solved.
I hope this helps.
=====EDIT=====
The solution to the problem is that (I think although it is not proven) get an https connection as it my observance that the php sdk does not work in an http connection. Look at my question here
======YEAH! THE FINAL AND CORRECT ANSWER AT LAST=======
This situation can only arise when cURL has been disabled by your web host(applicable for 100% of the cases of similar problem). Your website will run properly if cURL is installed. Usually, the error that users receive (if errorreporting is turned on) is
Fatal error: Uncaught CurlException: 7: couldn't connect to host thrown in /home/.../base_facebook.php on line ...
Make sure sandbox mode is disabled and recheck your settings again including appId, secret .
and try changing your login url to $loginUrl = $this->facebook->getLoginUrl($pram);
Solved it by moving the script to another subdirectory on the server...
/login/ became /facebook/login/
I haven't got the slightest clue why that makes a difference...
$facebook = new Facebook(array(
'appId' => '***************',
'secret' => '**************************',
'cookie' => false //add this and try
));
and try adding permission "read_stream"
check this after clearing your browser cookies..
Its Happen to me and when i check my apache log i found this :
And i found its an SSL problems when communicate with Facebook via PHP SDK (using Curl).
You have to set CURL_OPTS "CURLOPT_SSL_VERIFYPEER" to "false" .
Ex:
facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;

Categories