25

I'm getting the following error message

Facebook SDK returned an error: No URL set!

Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookSDKException' with message 'No URL set!' in /membri/csslab/sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php:83

Stack trace:

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 in /membri/csslab/sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php on line 83

when trying to login with Facebook credentials.

Everything works fine in local. I have properly set the domain in Facebook App.

The code I'm using is that suggested by Facebook's documentation:

<?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;
}
?>

UPDATE:

 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);
}
stochazesthai
  • 617
  • 1
  • 7
  • 20
  • check the `domain` in `facebook App`. you need to add domain what you are using. – Yash Mar 23 '16 at 07:50
  • Thanks @Yash! In my case, the domain is http://xxxx.altervista.org, right? – stochazesthai Mar 23 '16 at 07:52
  • right, you have to add domain name. – Yash Mar 23 '16 at 08:31
  • I set the right domain in facebook App, but I continue to get the same error message. – stochazesthai Mar 24 '16 at 17:39
  • Have you enabled `php_curl`? Can you check if [THIS](http://stackoverflow.com/a/19597365/1627271) helps? – choz Mar 25 '16 at 08:00
  • @choz, I saw that answer, but I don't get where I should put that line of code! – stochazesthai Mar 25 '16 at 08:01
  • @stochazesthai Idk if it might help. But you can try before you start initializing your Facebook API. So, it'd be before `$fb = new Facebook...` – choz Mar 25 '16 at 08:03
  • I'm getting an error: Fatal error: Class 'Facebook' not found in ... – stochazesthai Mar 25 '16 at 08:06
  • 2
    @stochazesthai I just noticed that you said everything worked fine in local. Then, you should start confirming on php extensions versions on your server whether it's a match with your local. IMO, there's nothing wrong in your code. – choz Mar 25 '16 at 08:23
  • https://developers.facebook.com/docs/php/Facebook/5.0.0 –  Mar 29 '16 at 11:33
  • @krishna ... what? – stochazesthai Mar 29 '16 at 11:34
  • @Dmitry I tried. It failed. Same error. – stochazesthai Mar 30 '16 at 13:55
  • sorry, there was bad advice. I remove them. And i'm tried run your code on my local machine with php_curl and without it. And i can't catch this exception. – Dmytro Mar 30 '16 at 13:56
  • But this exception should contain curl error code `throw new FacebookSDKException($this->facebookCurl->error(), $curlErrorCode);`. Can you view this code (maybe it's can help us to define problem)? – Dmytro Mar 30 '16 at 14:00
  • @Dmitry I updated my question providing the curl error code you were mentioning... I hope it will help... – stochazesthai Mar 30 '16 at 14:14
  • Sorry for my English. I mean error code (numeric code) which describe error. You can get it like this `$e->getCode();`. This is numeric code, and [here](https://curl.haxx.se/libcurl/c/libcurl-errors.html) you can find transcript for this code. – Dmytro Mar 30 '16 at 14:20
  • This error message typically occurs when the developer has not set a valid redirect URL in their Facebook App settings. The redirect URL is the location that the user will be directed to after they have successfully logged in with their Facebook account. This URL needs to be set in the App Dashboard under the Settings > Basic section. Make sure that the redirect URL is set correctly and that it matches the URL that the user is being directed to after the login attempt. – Amit Ghosh Anto Jan 14 '23 at 10:49

3 Answers3

0

OAuth helps to manage the access tokens you have to validate them

// 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');
Ankanna
  • 737
  • 2
  • 8
  • 21
0

I'm not sure if this helps, but you can use a package to handle social login (fb, twitter, google, ...) you might want to look at this PHP League package and use their library instead of using the facebook SDK.

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

Sometimes when you're really stuck, it helps to try things from a different angle.

jlos
  • 1,010
  • 1
  • 8
  • 12
0

check your callback url or OAuth url in facebook developer panel. And be sure that you put https:// at the beginning of callback url. This is really old bug of facebook... maybe this could help...

Berk Kanburlar
  • 260
  • 2
  • 11