0

I've got a bit of a weird issue going on with the new php-sdk and I can't seem to work it out.

I've got a phalconphp application where I present the user with the sign up view if they are not currently signed in, regardless of the url I present this view (Without redirecting the url)

When I set-up my FacebookRedirectLoginHelper I pass in the http host and the request uri, so that I can redirect the user back to the same page they intially tried to access e.g

  Facebook\FacebookRedirectLoginHelper('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

unfortunately this does not work. I always get an SDK exception telling me the redirect_uri isn't the one passed in. This is the case even if I am on the homepage e.g

  var_dump($_SERVER['REQUEST_URI']);
  returns "/"

However if I explicitly put the trailing slash in instead of the request_uri then it works correctly. e.g

 Facebook\FacebookRedirectLoginHelper('http://'.$_SERVER['HTTP_HOST'].'/');

I've even compared the 2 generated urls (again just on the index page so the path is simply "/") and they are exactly the same. The only issue appears to be trying to dynamically generate this. I can't for the life of me work out what is going on here. It doesn't seem to be any kind of double encoding and I'm just a bit stumped as to why this wouldn't work.

At first I thought it might be something to do with PhalconPHP and the routing but this doesn't seem to be the case as even a simple example fails.

An example simple php file is below. You will obviously need to include the sdk and set-up an app

 <?php
   ob_start();
   session_start();
   $appId = 'xxxxxxxxxxxxxxxxxxxxxxxx';
   $secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

   //require all the facebook stuff
   Facebook\FacebookSession::setDefaultApplication($appId,$secret);
   $helper = new Facebook\FacebookRedirectLoginHelper('http://'.$_SERVER['HTTP_HOST'] .'/'); //will work
   //$helper = new Facebook\FacebookRedirectLoginHelper('http://'.$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']); //won't work


// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );

 // validate the access_token to make sure it's still valid
 try {
   if ( !$session->validate() ) {
     $session = null;
   }   
  } catch ( Exception $e ) {
  // catch any exceptions
  $session = null;
 }
 }  

if ( !isset( $session ) || $session === null ) {
// no session exists

try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}

}

// see if we have a session
 if ( isset( $session ) ) {

 // save the session
 $_SESSION['fb_token'] = $session->getToken();
  // create a session using saved token or the new one we generated at login
   $session = new FacebookSession( $session->getToken() );

  // graph api request for user data
   $request = new FacebookRequest( $session, 'GET', '/me' );
 $response = $request->execute();
  // get response
   $graphObject = $response->getGraphObject()->asArray();

  // print profile data
   echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

  // print logout url using session and redirect_uri (logout.php page should destroy the session)
   echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';

} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '">Login</a>';
   }

The actual exception is :

Facebook\FacebookAuthorizationException Object ( [statusCode:Facebook\FacebookRequestException:private] => 400 [rawResponse:Facebook\FacebookRequestException:private] => {"error":{"message":"Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100}} 

 [responseData:Facebook\FacebookRequestException:private] => Array ( [error] => Array ( [message] => Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request [type] => OAuthException [code] => 100 ) ) [message:protected] => Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request
TommyBs
  • 9,354
  • 4
  • 34
  • 65
  • Did you set your host url or base url in fb app settings? – bhushya Jul 20 '14 at 11:55
  • Yeah my site url is set to be the current live version of the site and the and my app domains section lists the localhost version. Though regardless of that my simple example has all the correct localhost urls set-up e.g http://simple.localhost/ is the site url and my app domains just contains simple.localhost. And facebook always appends the / in the site url if you don't add it – TommyBs Jul 20 '14 at 12:05
  • can you also paste the fb exception here? – bhushya Jul 20 '14 at 12:08
  • I've updated the question with it – TommyBs Jul 20 '14 at 12:13
  • I worked it out, answer posted below – TommyBs Jul 20 '14 at 12:18
  • it seems like your domain is not matching with the domain in the app settings . ... also check this one ... http://stackoverflow.com/questions/16562602/facebook-redirect-url-issue-oauthexception – bhushya Jul 20 '14 at 12:19
  • No that was all fine as I say, I had a real simple example. The issue was the $_SERVER['REQUEST_URI'] was obviously different from what I sent as the querystring is appended on it when it comes back – TommyBs Jul 20 '14 at 12:28

1 Answers1

1

I've worked this out. It was stupid of me. When the login url is generated with $_SERVER['REQUEST_URI'] it obviously comes back as "/" on the second run through it is instantiated again, but this time has the returned query string in it, thus the redirected uri set in the helper class is no longer the same as the base uri. Basically I need the request uri without the querystring

so now I just use

  $_SERVER['HTTP_HOST']. strtok($_SERVER['REQUEST_URI'], '?')
TommyBs
  • 9,354
  • 4
  • 34
  • 65
  • 1
    THanks man. I had the same problem and this solved it. I was taking request_uri and when the facbook returned to the app, it sent code=xxxxx .. which is not the same with the initial url. That is why the error. :) – Gogol Dec 22 '14 at 05:32