I'm making a PHP application running on Google App Engine, and I'm trying to implement a Facebook Login.
After accessing to the login webpage, I am redirected to Facebook, and after I accept to log in for the application and I get redirected, I get the error "Error : Option 10065 is not supported by this curl implementation."
I have curl_lite enabled, and I'm running it on localhost for the time being. Here's my code:
<?php
session_start(); //Session should be active
#region Settings
$app_id = 'xxxxxxx'; //Facebook App ID
$app_secret = 'yyyyyyy'; //Facebook App Secret
$required_scope = 'public_profile'; //Permissions required
$redirect_url = 'http://localhost:8080/signup_fb.php'; //FB redirects to this page with a code
#endregion
#region Imports
//include autoload.php from SDK folder, just point to the file like this:
require_once "../libraries/facebook-php-sdk-v4-4.0-dev/autoload.php";
//import required class to the current scope
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;
#endregion
FacebookSession::setDefaultApplication($app_id , $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
//try to get current user session
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
die(" Error : " . $ex->getMessage());
}
if ($session){ //if we have the FB session
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
//do stuff below, save user info to database etc.
echo $user_profile->getProperty('name');
}else{
//display login url
$login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
echo 'Login with Facebook';
}
Thank you for your answers :)
It's understandable that with curl_lite enabled (curl interface backed by urlfetch and with fewer features) you would see such an issue. Check the docs as another commenter mentioned, and you should definitely be using the real curl if you intend to run such specific advanced functionality with your box's devserver as the client to facebook's login flow.
Related
I was looking some questions very similar to mine, but still lost in space here. I got no problems with JS part. I do the login and got my token. Now I need to send http request to a php that will perform queries in the database. How can I trust in a client request? I think I must have a confirmation of the user in server-side. So I taken the php SDK:
EDIT: This is the updated code, it's now working. The $fbToken in obtained by JavaScript and passed to php by Ajax request. Not using FacebookSession yet, baby steps...
$Login = ($_SESSION["Login"])? $_SESSION["Login"] : (object)array("ativo"=>FALSE, "metodo"=>"", "id"=>-1, "nome"=>"", "tipo"=>"", "publico"=>"", "nLogin"=>0, "captcha"=>"") ;
//
$fbPhpUser = FALSE;
require $Amb->phpBib.'Facebook/autoload.php';
//use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
//FacebookSession::setDefaultApplication('{myAppCode}', '{myAppSecret}');
$fb = new Facebook\Facebook([
'app_id' => '{myAppCode}',
'app_secret' => '{myAppSecret}',
'default_graph_version' => 'v2.3',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', $fbToken);
$fpPhpUser = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
fim("fbGraph", 'Erro Facebook Graph: ' . $e->getMessage());
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
fim("fbSDK", 'Erro Facebook SDK: ' . $e->getMessage());
exit;
}
if($fpPhpUser){
$Login->ativo = true;
$Login->metodo = "fb";
$_SESSION["Login"] = $Login;
}
The idea is to get a confirmation that the user is logged and Facebook has authorized my app, so '{access-token}' will be sent by post http request from client browser altogether with the rest of data (e.g.: user register form to be modified on my website database). $Login->ativo==TRUE means I can safely perform database queries. (the user can also login by my own website login system, but I wish he do by Facebook for an integration)
Not sure if will work and a bit tired of to try… Is this the right approach?
You only need the Javascript SDK to accomplish this. Your authorization is obtained through the statusChangeCallback function:
if (response.status === 'connected') {
//Logged into FB and app...
} else if (response.status === 'not_authorized') {
//Have NEVER logged into the app before.... you will see permissions page...
} else {
//NOT logged into FB... so not sure if logged into app...
}
Once you are connected, pass the access token via AJAX to your server and do whatever you want with it.
https://developers.facebook.com/docs/facebook-login/web#checklogin
I am experimenting with the Facebook Graph API and have this
require 'sdk/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication('<MY APP ID IS HERE>', '<MY APP SECRET IS HERE>');
$helper = new FacebookRedirectLoginHelper('http://whispershare.co.za/dashboard');
$loginUrl = $helper->getLoginUrl();
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
echo $ex;
} catch(\Exception $ex) {
echo $ex;
}
if ($session) {
echo 'yay';
} else echo 'boo';
I set the App ID and App secret but I keep getting the 'echo "boo";' on my page.
My app seems to be set up correctly. If it wasn't then it would give me an error in the try, right?
What could be wrong?
Please check my fb login whats is the error?
Here is the link.
It's say -
Graph returned an error: Can not open the link: Link domain is not included in the list of gadget domains.
I had this code and it was working fine for months. Suddenly, the production environment stopped working, where $helper->getSessionFromRedirect() is now always returning a NULL value.
login form page (index.php)
require 'vendor/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication(123456, 'a1b2c3d4e5');
$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');
$scope = array('email');
$loginUrl = $helper->getLoginUrl($scope);
echo 'Login';
login processing (login.php)
require 'vendor/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication(123456,'a1b2c3d4e5');
$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');
try {
# success
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
# when Facebook returns an error
} catch(\Exception $ex) {
# when validation fails or other local issues
}
if($session) {
# do something with user data
$me = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
# redirect to user profile page
header('Location: http://domain.com/profile.php');
exit();
} else {
# $session is always NULL with this code :(
}
This is working on my test App, but it's not working with my production App ID. The only difference between those are, of course, the App ID and Secret ID values. I already made sure I was following the Facebook instructions. Also, I'm using Facebook PHP SDK 4's last version.
After a long diagnosis, I found out that there was a discrepancy. Since I was using Cloudflare's SSL feature, the original $helper = new FacebookRedirectLoginHelper('http://domain.com/login.php'); request was made with http:80, but the 2nd $helper = new FacebookRedirectLoginHelper('http://domain.com/login.php'); was made with https:443 ... this returns a no match kind of signature to Facebook, which causes the NULL value in the $session variable.
TL;DR do not mix http and https when making requests to fb.
Ultimately I am attempting to display a public facebook page feed on the website belonging to the Facebook page's owner.
I am using PHP SDK 4.0 attempting to pull in a couple of posts from the page from graph api v2.2.
I have set up an app on developers.facebook.com, I am using the app id and app secret to setup the default application.
The method I use below to get the access token returns the App access token.
// Skip these two lines if you're using Composer
define('FACEBOOK_SDK_V4_SRC_DIR', __ROOT__.'/facebook/src/Facebook/');
require __ROOT__ . '/facebook/autoload.php';
session_start();
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\GraphPage;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
$page_id = "pageID";
$app_id = "appID";
$app_secret = "appSecret";
FacebookSession::setDefaultApplication( $app_id, $app_secret );
// Get access token
$access_token = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id={$app_id}&client_secret={$app_secret}&grant_type=client_credentials");
$access_token = substr($access_token, strpos($access_token, "=") + 1);
// Start new session
$session = new FacebookSession("$access_token");
// Get the GraphUser object for the current user:
try {
$request = new FacebookRequest($session, 'GET', "/{$page_id}/posts");
$response = $request->execute();
$graphObject = $response->getGraphObject();
var_dump($graphObject);
} catch (FacebookRequestException $e) {
echo $e;
// The Graph API returned an error
} catch (\Exception $e) {
// Some other error occurred
}
Both my code, and typing this information into the URL
https://graph.facebook.com/v2.2/pageID/feed?access_token=accessToken
returns
{
"data": [
]
}
Does anyone know why the dataset is empty?
How can I get the public posts of a page without having to ask the website visitor for authentication?
It's all in the docs, you know:
https://developers.facebook.com/docs/graph-api/reference/v2.2/page/feed/#readperms
Permissions
An access token is required to view publicly shared posts.
A user access token is required to retrieve posts visible to that person.
A page access token is required to retrieve any other posts.
This means that an App Access Token is not working. You either need a User or a Page Access Token.
Hello I am working with the sdk v4 facebook and when I login it does not redirect but generates the code ( In low image Link when I log ) Someone can help ?
Image
Code
<?php
session_start();
// Define the root directoy
define( 'ROOT', dirname( __FILE__ ) . '/' );
// Autoload the required files
require_once( ROOT . 'autoload.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookSDKException;
//Dados API
$app_id = '346646635512123';
$app_secret = '21h3vj12i312b3hj123';
$redirect_uri = 'http://localhost:8888/Facebook/';
// Permissoes
$permissions = array('email', 'user_location', 'user_birthday', 'manage_pages', 'publish_actions', 'user_photos');
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_uri);
try {
$session = $helper->getSessionFromRedirect();
} catch(Exception $ex) {
// When validation fails or other local issues
}
if (isset( $session)) {
echo'Estou Ligado';
}
else
{
echo "Login With Facebook";
}
?>
You don´t have any redirection in your code, that is why. You need to click on that login link to get to the authorization screen. I think you misunderstood "FacebookRedirectLoginHelper", it is usefuly AFTER a redirection from the authorization screen.
I would suggest using the JS SDK for authorization though. Much easier to handle, no redirection needed, no PHP needed, ...