"The redirect_uri URL must be absolute" Facebook PHP SDK 错误

"The redirect_uri URL must be absolute" error on Facebook PHP SDK

编辑:我在基本设置下将 http://www.example.com(例如我的实时站点)作为 站点 URL。我在 App Domains 下还有 www.example.comexample.com

我得到的错误是:

The redirect_uri URL must be absolute

当我尝试登录时,我的代码出现问题。这是我的 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>';

?>

为什么会出现这个错误?

您还需要添加重定向 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)。

Why is this error happening?

因为您在将 URL 传递给 getLoginUrl 方法之前对 URL 进行编码。

SDK 会在内部处理 - 所以现在您有一个 URL 已经 URL 编码 两次 ,这使得它无法识别作为有效的绝对 URL.

因此只需将 URL 传递给该方法 而无需 应用任何额外编码。


编辑:此外,方法 getLoginUrl 需要 两个 参数——第一个是重定向 URI,然后是第二个范围——而不是两个 [=22] =]一个数组。

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