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

27

当尝试使用Facebook凭据登录时,我收到以下错误消息:

Facebook SDK 返回错误:未设置URL!

致命错误:在/membri/csslab/sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php的第83行,未捕获异常'Facebook\Exceptions\FacebookSDKException',消息为'No URL set!'

堆栈跟踪:

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(Object(Facebook\FacebookRequest))

2 /membri/csslab/sdk/src/Facebook/Authentication/OAuth2Client.php(226):Facebook\Authentication\OAuth2Client->sendRequestWithClientParams('/oauth/access_t...',Array)

3 /membri/csslab/sdk/src/Facebook/Authentication/OAuth2Client.php(166):Facebook\Authentication\OAuth2Client->requestAnAccessToken(Array)

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

5 /membri/csslab/login-callback.php(30):Facebook\Hel在/membri/csslab/sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php的第83行上抛出了异常,

我在本地一切正常。我已经在Facebook应用程序中正确设置了域名。

我正在使用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);
}

检查Facebook应用程序中的域名。您需要添加您正在使用的域名。 - Yash
谢谢 @Yash!在我的情况下,域名是 http://xxxx.altervista.org,对吗? - stochazesthai
我在Facebook应用程序中设置了正确的域名,但我仍然收到相同的错误消息。 - stochazesthai
2
@stochazesthai 我刚刚注意到你说本地一切都正常。那么,你应该开始确认你的服务器上的php扩展版本是否与本地匹配。在我看来,你的代码没有任何问题。 - choz
@Dmitry 我试过了。失败了。同样的错误。 - stochazesthai
显示剩余12条评论
3个回答

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');

我遇到了一个错误:致命错误:在/membri/csslab/login-callback.php的第50行中,对非对象调用成员函数getValue()。 - stochazesthai
$accessToken = $helper->getAccessToken(); - Ankanna

0

请检查您在Facebook开发者面板中的回调URL或OAuth URL。确保在回调URL的开头加上https://。这是Facebook的一个很老的bug...也许这可以帮助解决问题...


0
我不确定这是否有所帮助,但你可以使用一个包来处理社交登录(fb、twitter、google等),你可以看看这个PHP League包,并使用他们的库代替使用facebook SDK。

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

有时候当你真的卡住了,尝试从不同的角度去尝试会有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接