0

我想用PHP創建FB登錄。 我的應用程序的URL是:http:// example.com。 重定向URL是http:// example.com/login/facebook/Facebook的PHP PHP登錄「錯誤的請求」

這是我在HTTP創建登錄URL代碼:// example.com:

$fb = new \Facebook\Facebook([ 
     'app_id'  => FB_APP_ID, 
     'app_secret' => FB_APP_SECRET, 
     'default_graph_version' => 'v2.2', 
    ]); 

    $callback = 'http:// example.com/login/facebook/'; 
    $helper = $fb->getRedirectLoginHelper(); 
    $permissions = ['email']; 
    $data['fb_url'] = $helper->getLoginUrl($callback, $permissions); 

我得到一個URL和將其設置爲用戶可以點擊並登錄的鏈接。

這是我在重定向URL代碼(HTTP:// example.com/login/facebook/):

$fb = new Facebook\Facebook([ 
     'app_id' => FB_APP_ID, // Replace {app-id} with your app id 
     'app_secret' => FB_APP_SECRET, 
     'default_graph_version' => 'v2.2', 
    ]); 

    $helper = $fb->getRedirectLoginHelper(); 

    try { 
     $accessToken = $helper->getAccessToken(); 
    } catch(Facebook\Exceptions\FacebookResponseException $e) { 
     // When Graph returns an error 
     echo 'Graph returned an error: ' . $e->getMessage(); 
     exit; 
    } catch(Facebook\Exceptions\FacebookSDKException $e) { 
     // When validation fails or other local issues 
     echo 'Facebook SDK returned an error: ' . $e->getMessage(); 
     exit; 
    } 

    if (! isset($accessToken)) { 
     if ($helper->getError()) { 
      header('HTTP/1.0 401 Unauthorized'); 
      echo "Error: " . $helper->getError() . "\n"; 
      echo "Error Code: " . $helper->getErrorCode() . "\n"; 
      echo "Error Reason: " . $helper->getErrorReason() . "\n"; 
      echo "Error Description: " . $helper->getErrorDescription() . "\n"; 
     } else { 
      header('HTTP/1.0 400 Bad Request'); 
      echo 'Bad request'; 
     } 
     exit; 
    } 

的代碼是從這裏的例子 - FB SDK DOCS, 但我得到這個錯誤:

Bad request

和訪問令牌= NULL。 錯誤在哪裏以及如何解決?

+0

Javascript fb登錄更容易。對於這種php方法,請確保您替換了應用程序ID和密碼,並且您在兩個文件的開頭都使用了session_start()。另外,您可以嘗試在這裏添加一些驗證:https://developers.facebook.com/docs/php/howto/example_facebook_login?locale = en_US – profimedica

回答

0

我想因爲GET參數的代碼和狀態不能在重定向頁面訪問。嘗試在重定向頁面中執行$ _GET的var_dump並查看它們是否在那裏。