Jerry McPhoton
Jerry McPhoton

Reputation: 75

Facebook php Oauth is not working

I'm new to facebook apps and php in general, and I have a bit of a problem. I cannot get OAuth to work correctly with my application. When you go the application itself, it does not redirect to the oAuth dialog. It merely displays a blank page that does nothing. If anyone can help me with this, I really need it haha. Thanks! So far, my code is as follows:

<?php

   include_once ('santatree/facebook.php');


   $app_id = '276853929000834';
   $application_secret = 'e3a12b11221f3fef1e06952e15fdc8e4';

   $facebook = new Facebook(array(
  'appId'  => $app_id,
  'secret' => $application_secret,
  'cookie' => true, // enable optional cookie support
));
?><?
if ($facebook->getSession())  
 {  
  $user = $facebook->getUser();  
 }  
else  
  {  
  $loginUrl = "https://www.facebook.com/dialog/oauth?    type=user_agent&display=page&client_id=276853929000834 
   &redirect_uri=http://apps.facebook.com/digitalsanta/&scope=user_photos";  
  header("Location: https://www.facebook.com/dialog/oauth?        type=user_agent&display=page&client_id=276853929000834         &redirect_uri=http://apps.facebook.com/digitalsanta/ &scope=user_photos");
  echo '';  
  }  

Upvotes: 3

Views: 3862

Answers (2)

ShawnDaGeek
ShawnDaGeek

Reputation: 4150

I do my redirects based on the session token.

This assumes that you will be using the most recent php-sdk 3.1.1 and have Oauth2 enabled in your app settings.

SAMPLE HERE: login / out url is in footer of plugin. http://apps.facebook.com/anotherfeed/TimeLineFeed.php?ref=facebook-stackoverflow


<?php
require './src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '',
  'secret' => '',
));
$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;
  }
}
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}
$access_token = $_SESSION['fb_135669679827333_access_token'];
if (!$access_token) {
echo '<script>';
echo 'top.location.href = "'.loginUrl.'";';
echo '</script>';
} else {
echo '<a href="'.logoutUrl.'">Logout</a>';
}
?>

https://developers.facebook.com/apps to edit your app.

  • If you do not have an app you will need to create one.
  • You will also need to set up the canvas and secure canvas urls to avoid errors.

enter image description here

Upvotes: 2

Raptor
Raptor

Reputation: 54260

You only defined the variable $loginUrl, but you haven't redirect user to go to the URL. Consider using

header("Location: $loginUrl");

to forward your user if you haven't sent your header yet.

Upvotes: 1

Related Questions