dongxing2263 2011-02-25 04:43
浏览 63

如何解析Facebook API中的身份验证代码? 获取MySQL错误

What's the right way to parse the code when authenticating your app during a user login? I'm using PHP for my app.

The reason I'm asking is because I keep getting weird queries in MySQL whenever I try to do inserts into my database. The API calls are all working, but if I try to put in user details (name, email, access_token) then three or four other queries will also run after that with empty values, each in different rows. This only happens when I try to get the authentication code via $_REQUEST['code'] or $_SERVER['QUERY_STRING'] after the dialog url.

What's weird though is that I don't get any issues with MySQL at all if I simply try calling the same page with $code hard-coded to the right value. Obviously, I can't leave it like that since the authentication code is always changing, but it's weird because only then do my MySQL work normally. Has anyone else experienced this? Is the code response encoded in a different format maybe?

Any help would be greatly appreciated, here's my code:

$code = $_REQUEST['code'];

if (empty($code)) {
  $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id .     
  "&redirect_uri=" . urlencode($redirect) . "&scope=offline_access,email";
  echo("<script> top.location.href='" . $dialog_url . "'</script>");
}

$response = urldecode($_SERVER['QUERY_STRING']);     
$code = substr($response,5);

$token_url = "https://graph.facebook.com/oauth/access_token?client_id=" 
            . $app_id . "&redirect_uri=" . urlencode($redirect) . "&client_secret=" .  
              $app_secret . "&code=" . $code;

$access_token = file_get_contents($token_url);
$user_url = "https://graph.facebook.com/me?" . $access_token;
$user = json_decode(file_get_contents($user_url));

$user_id = $user->id;
$email = $user->email;

// MYSQL insert queries into database
  • 写回答

1条回答 默认 最新

  • drtzb06222 2011-02-27 23:05
    关注

    Are you using the facebook php sdk file? or are you making your own authorization callback? because its much easyer, example:

    ////////////////////////////////////////////////////////
    require_once('facebook.php');
    
    $facebook = new Facebook(array(
      'appId'  => 'xxxxxxxxxxxxxxxxx',
      'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      'cookie' => true,
    ));
    $session = $facebook->getSession();
    
    $me = null;
    // Session based API call.
    if ($session) {
      try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
    
      } catch (FacebookApiException $e) {
        error_log($e);
      }
    }
    
    
    // login or logout url will be needed depending on current user state.
    if ($me) {
      $logoutUrl = $facebook->getLogoutUrl();
    
       $sql=my_sql_fetch_array(mysql_query("SELECT * from mytable where id='".$me['id']."'"));
    
    if($sql)
    {
     $_SESSION["usr_id"] =$me['id'];
      $_SESSION["usr_name"]=$me['name'];
    }
    else{
    mysql_query("insert into mytable (id,fbid,name) values(NULL,'".$me['id']."','".$me['name']."'");
    $_SESSION["usr_id"] =$me['id'];
      $_SESSION["usr_name"]=$me['name'];
    }
    
    } else {
      $loginUrl = $facebook->getLoginUrl();
    }
    
    
    //i set sessions here or maybe some mysql commands like check if the user is already in the database and then set the sessions.
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?