“The redirect_uri URL must be absolute” error on F

2019-07-16 04:09发布

问题:

EDIT: I have http://www.example.com (example being my live site) as the Site URL under basic settings. I also have www.example.com and example.comunder App Domains.

The error I'm getting is:

The redirect_uri URL must be absolute

My code breaks when I try to login. Here's my code for the getLoginUrl():

<?php

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

    $redirectURI = 'http://example.com/login-callback.php';
    $encodedRedirectURI = urlencode($redirectURI);

    $helper = $fb->getRedirectLoginHelper();
    $loginUrl = $helper->getLoginUrl(
            array(
                'scope' => 'ads_management,read_insights',
                'redirect_uri' => $encodedRedirectURI
            )
        );

    echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';

?>

Why is this error happening?

回答1:

Why is this error happening?

Because you are URL-encoding the URL, before passing it to the getLoginUrl method.

The SDK takes care of that internally - so now you have a URL that has been URL-encoded twice, and that makes it unrecognizable as a valid, absolute URL.

So just pass the URL to that method without applying any extra encoding.


Edit: Additionally, the method getLoginUrl expects two parameters - first the redirect URI, and then the scope as the second one - not both as one array.

$loginUrl = $helper->getLoginUrl(
    $redirectURI,
    array('ads_management', 'read_insights')
);


回答2:

You will also need to add your redirect URL (http://example.com/login-callback.php) as "Valid OAuth redirect URI" in the advanced settings of your facebook app (https://developers.facebook.com/apps).