“The redirect_uri URL must be absolute” error on F

2019-07-16 03:28发布

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?

2条回答
forever°为你锁心
2楼-- · 2019-07-16 04:04

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')
);
查看更多
神经病院院长
3楼-- · 2019-07-16 04:25

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).

查看更多
登录 后发表回答