2

我开发了一个网站,我想在其中加入“使用 facebook 登录”按钮。我已经编写了必要的部分并为它制作了 facebook 应用程序。在我的代码中,我什至传递了应用程序 ID 和密钥。即使这样做之后,当我按下按钮通过 facebook 登录时,它也会给我一个错误“需要参数 app_id”。这是我的登录脚本

    <?php 
            include 'fbaccess.php'
            if(empty($user))
            {
            ?>
            <form name="fblogin" action="<?php echo $loginUrl;?>">
            <input type="submit" name="fbsubmit" value="Login with Facebook"/>
            </form>
            <?php                   
            } 
            else {
                echo $user_info;
            }
            ?>

这是我的 fbaccess.php 代码

    <?php
     $app_id        = APP_ID;
     $app_secret    = APP_SECRET;
     $site_url  = "www.jajabora.com/index.php";

     include_once "src/facebook.php";

     //creating the object of facebook from the API
     $facebook = new Facebook(array(
 'appId'        => $app_id,
 'secret'   => $app_secret,
  ));

      //getting the user id to check whether the user is logged in or not
       $user = $facebook->getUser();

      //if user is not authenticated api/me will throw an exception, hence we will know                he isnt logged in after logging out
      /*checks if the user is logged in or not*/if($user){
      // Single query method 
  try{
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
   }/*if exception the user has logged out after logging in hence not authenticated*/
  catch(FacebookApiException $e){
    error_log($e);
    $user = NULL;
  }
      // Single query method ends 
      }
       if($user){
  // Get logout URL
  $logoutUrl = $facebook->getLogoutUrl();
      }else{
  // Get login URL
  $loginUrl = $facebook->getLoginUrl(array(
    'scope' => 'publish_stream, user_birthday,user_location,email,first_name,last_name,gender',
    ));
       }
      if($user){
  // Proceed knowing you have a logged in user who has a valid session.

      //========= Batch requests over the Facebook Graph API using the PHP-SDK ========
  // Save your method calls into an array
  $queries = array(
    array('method' => 'GET', 'relative_url' => '/'.$user)/*,
    array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'),
    array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
    array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'),*/
    );

   // POST your queries to the batch endpoint on the graph.
   try{
    $batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
     }catch(Exception $o){
    error_log($o);
      }

    //Return values are indexed in order of the original array, content is in   ['body'] as a JSON
   //string. Decode for use as a PHP array.
    $user_info      = json_decode($batchResponse[0]['body'], TRUE);
    $feed           = json_decode($batchResponse[1]['body'], TRUE);
    /*$friends_list     = json_decode($batchResponse[2]['body'], TRUE);
    $photos         = json_decode($batchResponse[3]['body'], TRUE);*/
        //========= Batch requests over the Facebook Graph API using the PHP-SDK ends =====

       try {
   $publishStream = $facebook->api("/$user/feed", 'post', array(
    'message'       => 'Check out jajabora.com',
    'link'          => 'http://jajabora.com'/*,
    'picture'       => 'http://25labs.com/images/25-labs-160-160.jpg'*/,
    'name'          => 'Jajabora',
    'caption'       => 'jajabora.com',
    'description'       => 'A carpooling website. highly recommended to  save fuel and cost of travel',
    ));
           }catch(FacebookApiException $e){
         error_log($e);
               }
                }
              ?>

请指导我。我是 facebook 登录编码的新手

4

1 回答 1

1

我怀疑问题可能隐藏在这里:

<form name="fblogin" action="<?php echo $loginUrl;?>">
        <input type="submit" name="fbsubmit" value="Login with Facebook"/>
</form>

您正在向生成的链接发送获取请求,为什么?用户应该遵循该链接并授予您的应用程序权限,尝试这样的事情而不是表单:

<a href="<?php echo $loginUrl?>">Login with Facebook </a>

为了将用户重定向到某个页面,请将 redirect_uri 添加到 getLoginUrl:

$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream, user_birthday,user_location,email,first_name,last_name,gender',
redirect_uri => PAGE_URL
));

确保此页面属于您在应用设置中设置的域。

于 2013-03-28T10:10:51.913 回答