登录服务器端应用程序

时间:2013-01-10 11:44:55

标签: php facebook

我尝试使用facebook开发人员提供的步骤:

http://developers.facebook.com/docs/howtos/login/server-side-login/

但是,重定向后,它无法重定向回基本的php页面。我的代码有什么问题吗?

这是我的代码:

require_once 'library/facebook.php';
    $facebook = new Facebook(array(
    'appId'  => '*** My APP ID ***',
    'secret' => '*** My Secret ***',                
    'fileUpload' => true
));

$my_url = "https://apps.facebook.com/carnumberchecking";
session_start();

$user = $facebook->getUser();
$me = null;

if($user)
{
$uid = $facebook->getUser();
}else{
$url = $facebook ->getLoginUrl( array (
'scope' => 'publish_stream',
'req_perms' => 1,
'fbconnect' => 0
 ));
 echo "";                
}                  

$code = $_REQUEST["code"];
if(empty($code)) {  
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection  
$dialog_url = "https://www.facebook.com/dialog/oauth?    client_id=187384241402698&redirect_uri=" . urlencode($my_url) . "&state="
                . $_SESSION['state'] . "&scope=user_birthday,read_stream";
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}

if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
echo "1";
$token_url = "https://graph.facebook.com/oauth/access_token?"
        . "client_id=187384241402698&redirect_uri=" . urlencode($my_url)
        . "&client_secret=4e45477f9ed79f95fc4fd3af8f8a579d&code=" . $code;

$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
}

1 个答案:

答案 0 :(得分:1)

似乎你正在混合你在互联网上从各种来源找到的东西......几乎是过时的教程......等等。

如果您打算使用官方的Facebook PHP-SDK,那么这是登录过程的直接示例:https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php

否则,按照您在上面发布的链接,如果您只是编译上面的示例代码段,您应该有一个完整的工作示例!!:

<?php 

   $app_id = "YOUR_APP_ID";
   $app_secret = "YOUR_APP_SECRET";
   $my_url = "YOUR_URL";

   session_start();

   $code = $_REQUEST["code"];

   if(empty($code)) {
     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
     $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
       . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
       . $_SESSION['state'] . "&scope=read_stream";

     echo("<script> top.location.href='" . $dialog_url . "'</script>");
   }

   if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $app_secret . "&code=" . $code;

     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);

     $_SESSION['access_token'] = $params['access_token'];

     $graph_url = "https://graph.facebook.com/me?access_token=" 
       . $params['access_token'];

     $user = json_decode(file_get_contents($graph_url));
     echo("Hello " . $user->name);
   }
   else {
     echo("The state does not match. You may be a victim of CSRF.");
   }

您需要在此处放置APP_ID,APP_SECRET和重定向网址...您也可能需要更改权限(范围)或将其全部删除。