Facebook PHP SDK - getUser()突然返回0 [英] Facebook PHP SDK - getUser() suddenly returns 0

查看:206
本文介绍了Facebook PHP SDK - getUser()突然返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,为了解决这个问题,我花了几个小时在StackOverflow上看过这个问题的答案,并试图自己解决这个问题,但是它不再工作了。所以无论如何...



昨天,一切都很好。睡觉,醒来,用Facebook登录,在我的网站上没有工作了。登录链接发送到Facebook页面,需要我批准应用程序,但是在将我重定向到网站后,它不会登录我。我很快地跟踪问题getUser()总是返回0.所以我设置一个新的(测试)应用程序与一个非常基本的代码,只是为了看看是否有效。没有这是代码:

 <?php 

session_start();

require_once(facebook.php);

$ config = array();
$ config ['appId'] ='199971880127910'; //测试应用程序,所以我不介意显示这些
$ config ['secret'] ='e065588cb1edd96f821de8b3d362c488';
$ config ['trustForwarded'] = true;

$ facebook = new Facebook($ config);

try {
$ user = $ facebook-> getUser();
var_dump($ user);

} catch(Exception $ e){
$ e-> getMessage();
}

$ loginUrl = $ facebook-> getLoginUrl(array('redirect_uri'=>'http://gtcrais.redirectme.net/test/'));

echo< a href ='$ loginUrl'>登录< / a>;
?>

第二个答案在这里抓住了我的眼睛。我尝试了这个解决方案,它没有起作用,所以我打开了base_facebook.php并滚动到代码被传递给一些变量行458的行:



< blockquote>

$ code = $ this-> getCode();


我回应这个,它正确显示从URL传递的代码参数。有问题的行证明是下面那几行:


$ access_token = $ this-> getAccessTokenFromCode($ code);


当传递$ code变量时,此函数将返回值为$ access_token的值。在这个函数中,这部分代码抛出一个异常:

  try {
//需要绕过json_decode直接调用_oauthRequest
//,因为响应不是JSON格式。
$ access_token_response =
$ this-> _oauthRequest(
$ this-> getUrl('graph','/ oauth / access_token'),
$ params = array 'client_id'=> $ this-> getAppId(),
'client_secret'=> $ this-> getAppSecret(),
'redirect_uri'=> $ redirect_uri,
'code'=> $ code));
} catch(FacebookApiException $ e){
//很可能该用户最近被撤销了授权。
//无论如何,我们没有访问令牌,所以说。
print_r($ e);
返回false;
}

print_r($ e)显示一个巨大的乱七八糟(我有点一个noob,所以我几乎看不到任何有意义的东西)。我唯一看到重复的是


SSL证书问题,验证CA证书是否正常。详细信息:错误:14090086:SSL


这是我进行调试的距离。任何帮助是赞赏。昨天,一切都正常。今天,没有一行代码改变,它不再工作了。心烦意乱=(哦,是的,我正在用PHP 5.4.3运行WAMP。



编辑:这是错误信息: http://i.imgur.com/TeY8h.png



EDIT2:已解决:



在base_facebook.php中,在$ CURL_OPTS下添加以下2行:

  CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2

哪个使它...

  public static $ CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER = > true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT =>'facebook-php-3.2',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
);

我不知道这是多少安全问题,但它使Facebook的登录工作,所以你有它(和我有它在这个,fml)。



EDIT3:新的SDK发布,修复了这个问题: https://github.com/facebook/facebook-php-sdk

解决方案

在base_facebook.php中,在$ CURL_OPTS下添加以下2行:

  CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2

哪个使它...

  public static $ CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT =>'facebook-php-3.2',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
);

我不知道这是多少安全问题,但它使用Facebook登录工作,所以你有它。



编辑:新的SDK发布,修复了这个问题:https://github.com/facebook/facebook-php-sdk


First of all, to get this out of the way, I've spent hours looking through answers to this question here on StackOverflow, and tried to solve it on my own, but it just isn't working (anymore). So anyway...

Yesterday, everything worked fine. Went to bed, woke up, login with facebook didn't work anymore on 2 of my websites. The login link sends me to the Facebook page which requires me to approve the App, but after it redirects me back to the site, it doesn't log me in. I quickly tracked the problem to getUser() always returning 0. So I set up a new (test) app with a very very basic code, just to see if that works. It doesn't. Here's the code:

<?php

        session_start();

        require_once("facebook.php");

        $config = array();
        $config['appId'] = '199971880127910'; //test app so I don't mind showing these
        $config['secret'] = 'e065588cb1edd96f821de8b3d362c488';
        $config['trustForwarded'] = true;

        $facebook = new Facebook($config);

        try {
            $user = $facebook->getUser();
            var_dump($user);

        } catch (Exception $e) {
            $e->getMessage();
        }

        $loginUrl = $facebook->getLoginUrl(array('redirect_uri' => 'http://gtcrais.redirectme.net/test/'));

        echo "<a href='".$loginUrl."'>Login</a>";
?>

The second answer in here caught my eye. I tried that solution and it didn't work, so I opened up base_facebook.php and scrolled to the line where that 'code' is passed to some variable - line 458:

$code = $this->getCode();

I echo'd this, and it displayed correctly the code parameter that was passed from the URL. The problematic line turns out to be the one couple rows below that one:

$access_token = $this->getAccessTokenFromCode($code);

This function returns a value of false to $access_token when passed the $code variable. In that function, this part of the code throws an exception:

try {
      // need to circumvent json_decode by calling _oauthRequest
      // directly, since response isn't JSON format.
      $access_token_response =
        $this->_oauthRequest(
          $this->getUrl('graph', '/oauth/access_token'),
          $params = array('client_id' => $this->getAppId(),
                          'client_secret' => $this->getAppSecret(),
                          'redirect_uri' => $redirect_uri,
                          'code' => $code));
    } catch (FacebookApiException $e) {
      // most likely that user very recently revoked authorization.
      // In any event, we don't have an access token, so say so.
      print_r($e);
      return false;
    }

print_r($e) displays a huge blob of gibberish (I'm somewhat of a noob so I can't read almost anything from it that makes sense). The only thing that I see repeating in there is

SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL

And this is how far I've come with debugging this. Any help is appreciated. Once again, yesterday, everything was working fine. Today, without a single line of code changed, it doesn't work anymore. Mind boggling =( Oh yeah, I'm running WAMP with PHP 5.4.3

EDIT: Here's the error message: http://i.imgur.com/TeY8h.png

EDIT2: Solved:

In base_facebook.php add the following 2 lines under $CURL_OPTS:

CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2

Which makes it...

public static $CURL_OPTS = array(
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_USERAGENT      => 'facebook-php-3.2',
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2
  );

I'm not sure how much of a security issue this is, but it makes the Login with Facebook work, so there you have it (and there I have it. Entire day lost on this, fml).

EDIT3: New SDK released which fixes this problem: https://github.com/facebook/facebook-php-sdk

解决方案

In base_facebook.php add the following 2 lines under $CURL_OPTS:

CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2

Which makes it...

public static $CURL_OPTS = array(
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_USERAGENT      => 'facebook-php-3.2',
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2
  );

I'm not sure how much of a security issue this is, but it makes the Login with Facebook work, so there you have it.

EDIT: New SDK released which fixes this problem: https://github.com/facebook/facebook-php-sdk

这篇关于Facebook PHP SDK - getUser()突然返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆