I am trying to get Facebook login with their new PHP SDK 4.0 working for my site. I followed their gist almost verbatim and still can't even get a test page to work. When I try to log in, I get redirected to a url of the form:
https://www.facebook.com/v2.0/dialog/oauth?client_id={some number}&redirect_uri{localhost%2F%7E{MyName}%2F{my site}}&state=08d94ec4670256aa2b2c586781590766&sdk=php-sdk-4.0.0&scope=
I have filled out the same url on my Facebook developer page already, and this is the code I am trying to test:
<?php
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( '{My app ID}','{My app secret}' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('localhost/~{My Name}/{My Project}' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
} else {
// show login url
echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}
Unfortunately, I never get redirected back, or even load the login page. What could I be doing wrong here?
I had the same problem. You need to set a domain URL and site URL in your Facebook App settings first. Go to this page to change your setting: https://developers.facebook.com/apps.
I have shared a step by step here: http://bit.ly/1iNNsBy
You don't have to include the php files. Facebook wants you to use composer to manage what is required.
However.... they give you a workaround: Include autoload.php
and keep the file structure they give you for easiest implementation. For ease, put all the files from the SDK into a folder called facebook
, so you have a sub folder in there called src
and within that another one called Facebook
Then the SDK creates a link that your users can click on to authorize access to your app.
If you are testing, you need to keep deleting the app from your facebook profile to get the signup window each time (Settings
top right dropdown, then Apps
on left as of Aug 2014), otherwise it will bounce straight through to your specified redirect page and will appear as if nothing worked.
// if you include this file you don't need to use composer
require_once('facebook/autoload.php');
// use these
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
// initiate the session
FacebookSession::setDefaultApplication('[YOUR ID]', '[YOUR SECRET]');
// use the helper
$helper = new FacebookRedirectLoginHelper('[YOUR REDIRECT URL]');
// get a loginurl
$loginUrl = $helper->getLoginUrl();
echo "<a href='".$loginUrl."'>Link here</a>";
Notes to check:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With