Jea
Jea

Reputation: 484

Facebook php example - new user unable to authorize app

I am trying to make a php facebook app. Downloaded the Facebook php sdk and used the example.php (with my own appId and secret) to try to get an example app running. I can access the example app on facebook, but if a new user that has not authorized the app clicks on the link "Login with Facebook" in the facebook app, nothing happens. If they copy/paste the "Login with Facebook" link to the address bar in a web browser and press enter, they are asked to authorize the app. If they authorize the app it will work in facebook from then on.

This is the format of the link behind "Login with Facebook":

https://www.facebook.com/dialog/oauth?client_id=14219214211111&redirect_uri=https://mywebsite.co/?fb_source=search&ref=br_tf&state=6c16ccff1fa68a08b08c80e879911111&sdk=php-sdk-3.2.2 //Changed the client id, website and state

Below is the code for example.php.

Any help would be greatly appreciated.

<?php
require 'facebook.php';
$facebook = new Facebook(array(
  'appId'  => 'myappid',
  'secret' => 'mysecretid',
  'cookie' => true,
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $statusUrl = $facebook->getLoginStatusUrl();
  $loginUrl = $facebook->getLoginUrl();
}

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Check the login status using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo urldecode($statusUrl); ?>">Check the login status</a>
      </div>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo urldecode($loginUrl); ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>
  </body>
</html>

Edit:

I got the authorization working when I redirected the authorization to my own website, but now I am uncertaina about how to redirect back to the facebook app from my own website (since I don't want the user to stay there). Below is the changed code I used to get authorization to work:

} else {
   try{
       $statusUrl = $facebook->getLoginStatusUrl();
       $loginUrl = $facebook->getLoginUrl(
        array(
            'canvas' => 1,
            'fbconnect' => 1,
            'req_perms' => 'publish_stream'
        )
    );
    echo '<script>top.location="'.$loginUrl.'";</script>';
   } catch (Exception $e) {
       echo $e->getTraceAsString();
  }

Edit:

Now I got the redirection to work from my site back to the facebook app. It works! Just needed to use this code for the parameters of the getLoginUrl():

$loginUrl = $facebook->getLoginUrl(
    array(
        'canvas' => 1,
            'fbconnect' => 0,
            'req_perms' => 'publish_stream, email, offline_access',
            'redirect_uri' => 'https://apps.facebook.com/myfacebookapp/'
        )
    );

Upvotes: 0

Views: 933

Answers (1)

C3roe
C3roe

Reputation: 96325

So your app is a canvas or page tab app on Facebook? Then you have to add target="_top" to your login link, because the login dialog can not be displayed inside iframes.

Upvotes: 1

Related Questions