Facebook通过PHP SDK v4登录。返回"无效的redirect_uri"

时间:2015-01-04 23:15:46

标签: php facebook laravel-4 facebook-php-sdk facebook-login

我正在尝试将Facebook登录到我拥有的网站。 Web应用程序是使用Laravel 4.2构建的,我使用的是SammyK/LaravelFacebookSdk

我有以下代码用于登录的初始路由:

 Route::get('/facebook', function()
 {

      $login_link = Facebook::getLoginUrl(['email', 'user_status'], 'http://staging.breathe.yogijii.com/facebook/login/');
      return Redirect::to($login_link);

 });

die上执行$login_link会给我以下字符串:

  

https://www.facebook.com/v2.2/dialog/oauth?client_id=479112728894729&redirect_uri=http%3A%2F%2Fstaging.breathe.yogijii.com%2Ffacebook%2Flogin%2F&state=fd1c84414631fd179b3632f71cec9299&sdk=php-sdk-4.0.14&scope=email%2Cuser_status

回叫路线(facebook / login)是:

 // Endpoint that is redirected to after an authentication attempt
 Route::get('/facebook/login', function()
 {
    /**
    * Obtain an access token.
    */   
    try
    {
        $token = Facebook::getTokenFromRedirect();

         if ( ! $token)
         {
             return Redirect::route('login')
                ->with('flash_notice', 'Unable to obtain access token');
         }
    }
    catch (FacebookQueryBuilderException $e)
    {
         return Redirect::route('login')->with('flash_notice', $e->getPrevious()->getMessage());
    }

    if ( ! $token->isLongLived())
    {
        /**
        * Extend the access token.
        */
        try
        {
            $token = $token->extend();
        }
        catch (FacebookQueryBuilderException $e)
        {
           return Redirect::route('login')
                ->with('flash_notice', $e->getPrevious()->getMessage());
        }
    }

    Facebook::setAccessToken($token);

    /**
    * Get basic info on the user from Facebook.
    */
    try
    {
        $facebook_user = Facebook::object('me')->fields('id','name')->get();
    }
    catch (FacebookQueryBuilderException $e)
    {
        return Redirect::route('login')
            ->with('flash_notice', $e->getPrevious()->getMessage());
    }

    // Create the user if not exists or update existing
    $user = User::createOrUpdateFacebookObject($facebook_user);

    // Log the user into Laravel
    Facebook::auth()->login($user);

    return Redirect::route('login')
        ->with('flash_notice', 'Successfully logged in with Facebook');
});

代码重定向到Facebook,似乎工作正常。但是,当它返回到我的应用程序时,我收到错误:

  

无效的redirect_uri:应用程序配置不允许使用URL。

我已经查看了所有内容并花了好几个小时。我不确定我是否遗漏了一些小事。这就是我的应用配置:

Facebook settings screenshot

我的应用领域完美匹配。我也尝试制作一个画布应用程序,但也没用。有没有人有任何建议?

我尝试将重定向添加到有效的誓言中......仍然无法正常工作。 enter image description here

3 个答案:

答案 0 :(得分:1)

我想我解决了自己的问题...

我正在考虑@ CBroa上面关于成功重定向后无效重定向的错误消息的评论。这导致我看看错误消息发生了什么。

要获取我正在执行的错误消息:

$e->getPrevious()->getMessage()

这让我相信" getPrevious"不再相关,也许一切正常,但我正在访问令牌错误。我通过以下方式获取令牌:

Facebook::getTokenFromRedirect();

我做了一些研究,发现这个功能正在使用重定向网址。它正在构建错误的重定向URL作为localhost的东西。这是返回null,因为它不是我的重定向url。

所以在我的app / config / app.php中找到的关于它是如何构建这个url的阅读方法我有:

return array(

....

/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/

'url' => 'http://localhost:8888',

 .....

 );

这是错误的地方......我想我在最初的Laravel安装后从未更新过。在将其修复为正确的URL后,我开始获取令牌。

谢谢大家的帮助:)

答案 1 :(得分:0)

尝试应用域名应为yogijii.com,网站网址应为yogijii.com

请参阅: https://stackoverflow.com/a/11932194/1984156

答案 2 :(得分:0)

您将有效OAuth重定向URI设置为http://staging.breathe.yogijii.com/facebook/login/,因此我们要将您的网站网址设置为http://staging.breathe.yogijii.com/facebook/login/

您的网站网址应该是登录交易完成的地方,对于大多数意图和目的而言,您的重定向网址都是。

通过更改

中的重定向网址自行尝试

https://www.facebook.com/v2.2/dialog/oauth?client_id=479112728894729&redirect_uri=http%3A%2F%2Fstaging.breathe.yogijii.com%2Ffacebook%2Flogin%2F&state=fd1c84414631fd179b3632f71cec9299&sdk=php-sdk-4.0.14&scope=email%2Cuser_status

https://www.facebook.com/v2.2/dialog/oauth?client_id=479112728894729&redirect_uri=http%3A%2F%2Fstaging.breathe.yogijii.com&state=fd1c84414631fd179b3632f71cec9299&sdk=php-sdk-4.0.14&scope=email%2Cuser_status