2015-01-04 55 views
0

我想實現Facebook登錄到我有一個網站。該Web應用程序使用Laravel 4.2構建,我使用的是SammyK/LaravelFacebookSdkFacebook通過PHP SDK v4登錄。返回「無效的redirect_uri」

我已經爲這並登錄的初始路徑下面的代碼:對$login_link做一個die

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

     $login_link = Facebook::getLoginUrl(['email', 'user_status'], 'http://staging.breathe.yogijii.com/facebook/login/'); 
     return Redirect::to($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

回調路線(臉譜/登錄)爲:

// 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和似乎工作正常。然而,當談到回我的應用程序時出現錯誤:

Invalid redirect_uri: Given URL is not allowed by the Application configuration.

我已經看過了一切,在這花了幾個小時。我不確定我是否缺少一些小東西。這是我的應用程序的配置是這樣的:

Facebook settings screenshot

我的應用程序域完全匹配。我也嘗試製作一個畫布應用程序,也沒有工作。有沒有人有什麼建議?

我試着將我的重定向添加到有效的誓言......仍然無法正常工作。 enter image description here

+0

這將是很好的,你說爲什麼下投票,所以我可以改善這個問題...謝謝 – w3bMak3r

+1

如果您REDIRECT_URI錯了,你應該得到的是錯誤信息甚至在顯示auth對話框之前,但這不會發生。它允許我授權您的應用程序,然後重定向到它。 (然後你的應用說,它無法獲取訪問令牌。)因此,無論出現什麼問題,在這一步似乎都沒有出錯,但之後。 – CBroe

+0

這就是我的想法......奇怪的是它成功重定向後發生的錯誤。你有什麼想法嗎?我一直在看這個好幾個小時。感覺我缺少一些小東西。 – w3bMak3r

回答

1

試試吧,我想我解決我自己的問題......

我思考@ CBroa的上面有關成功重定向後發生的無效重定向錯誤消息的評論。這讓我想看看錯誤消息發生了什麼。

爲了得到錯誤信息我做:

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

這使我相信,「getPrevious」不再是相關的,也許一切工作,但我訪問令牌錯誤。我得到的令牌通過:

Facebook::getTokenFromRedirect(); 

我做了一些研究,發現這個功能是使用重定向url。這是建立錯誤的localhost重定向URL的東西。這是返回null,因爲它不是我的重定向url。

它是如何構建這個網址我在我的應用程序/配置/ app.php我找到了這樣讀futhur:

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後,我開始獲取令牌。

謝謝大家對你的幫助:)

0

您設定有效的OAuth重定向URI作爲http://staging.breathe.yogijii.com/facebook/login/所以我'd想要相信你設置你的網站URL爲http://staging.breathe.yogijii.com/facebook/login/

你的網站URL應該是登錄交易完成的地方,它用於大多數意圖和目的你的重定向URL。

自己通過改變重定向URL從

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

+0

謝謝......我試過了......原來這是在我的應用程序配置laravel設置中的一個問題。我指向本地主機而不是我的實際網址。 – w3bMak3r