登录尝试期间Facebook SDK错误:未设置URL

时间:2016-03-23 07:46:28

标签: php curl facebook-php-sdk

我收到以下错误消息

  

Facebook SDK返回错误:没有设置网址!

     

致命错误:未捕获   例外'Facebook \ Exceptions \ FacebookSDKException',消息'否   网址设置!'在   /membri/csslab/sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php:83

     

堆栈追踪:

     

0 /membri/csslab/sdk/src/Facebook/FacebookClient.php(216):Facebook \ HttpClients \ FacebookCurlHttpClient-> send('https://graph.f ...',   'GET','',Array,60)

     

1 /membri/csslab/sdk/src/Facebook/Authentication/OAuth2Client.php(277):   Facebook的\ FacebookClient-> sendRequest将(对象(实\ FacebookRequest))

     

2 /membri/csslab/sdk/src/Facebook/Authentication/OAuth2Client.php(226):   Facebook的\认证\ OAuth2Client-> sendRequestWithClientParams( '/ OAuth的/ access_t ...',   阵列)

     

3 /membri/csslab/sdk/src/Facebook/Authentication/OAuth2Client.php(166):   Facebook的\认证\ OAuth2Client-> requestAnAccessToken(阵列)

     

4 /membri/csslab/sdk/src/Facebook/Helpers/FacebookRedirectLoginHelper.php(255):   Facebook的\认证\ OAuth2Client-> getAccessTokenFromCode( 'AQAAf_xMnFr0C7i ...',   'http://csslab.a ...')

     

5 /membri/csslab/login-callback.php(30):   Facebook \ Hel in   /membri/csslab/sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php   在第83行

尝试使用Facebook凭据登录时。

一切都在当地运作良好。我已经在Facebook App中正确设置了域名。

我正在使用的代码是Facebook的文档中提供的代码:

<?php // login-callback.php
session_start();

function redirect($url, $permanent = false) {
    if (headers_sent() === false) {
        header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
    }
    exit();
}

$path = '';
$uri = 'http://xxxxx.altervista.org';
require_once $path . 'sdk/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
    'app_id' => 'xxxxxxxxxxx',
    'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxx',
    'default_graph_version' => 'v2.5',
    ]);

$helper = $fb->getRedirectLoginHelper();

try {

    $accessToken = $helper->getAccessToken();

} catch(Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

if (isset($accessToken)) {
    // Logged in!
    $_SESSION['facebook_access_token'] = (string) $accessToken;

    // Now you can redirect to another page and use the
    // access token from $_SESSION['facebook_access_token']
    redirect($uri . 'survey.php?token='.$_SESSION['facebook_access_token'] ,   false);
} elseif ($helper->getError()) {
    // There was an error (user probably rejected the request)
    echo '<p>Error: ' . $helper->getError();
    echo '<p>Code: ' . $helper->getErrorCode();
    echo '<p>Reason: ' . $helper->getErrorReason();
    echo '<p>Description: ' . $helper->getErrorDescription();
    exit;
}
?>

更新:

 public function send($url, $method, $body, array $headers, $timeOut)
{
$this->openConnection($url, $method, $body, $headers, $timeOut);
$this->sendRequest();

if ($curlErrorCode = $this->facebookCurl->errno()) {
    throw new FacebookSDKException($this->facebookCurl->error(), $curlErrorCode);
}

// Separate the raw headers from the raw body
list($rawHeaders, $rawBody) = $this->extractResponseHeadersAndBody();

$this->closeConnection();

return new GraphRawResponse($rawHeaders, $rawBody);
}

3 个答案:

答案 0 :(得分:0)

OAuth有助于管理您必须验证的访问令牌

// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);

// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId({app-id}); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();

if (! $accessToken->isLongLived()) {
  // Exchanges a short-lived access token for a long-lived one
  try {
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
    echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
    exit;
  }

  echo '<h3>Long-lived</h3>';
  var_dump($accessToken->getValue());
}

$_SESSION['fb_access_token'] = (string) $accessToken;

// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');

答案 1 :(得分:0)

我不确定这是否有帮助,但是您可以使用一个程序包来处理社交登录(fb,twitter,google,...),您可能希望查看此PHP League程序包并使用其库而不是使用facebook SDK。

https://github.com/thephpleague/oauth2-client

有时候,当您真正陷入困境时,可以从另一个角度尝试尝试。

答案 2 :(得分:0)

在 facebook 开发者面板中检查您的回调网址或 OAuth 网址。并确保将 https:// 放在回调 url 的开头。这真的是 facebook 的老错误...也许这可以帮助...