2016-04-29 46 views
0

我登錄Facebook登錄錯誤,有時我看到:Facebook的API登錄錯誤 - 會話已過期驗證訪問令牌

OAuthException: Error validating access token: 
Session has expired on Friday, 29-Apr-16 14:00:00 PDT. 
The current time is Friday, 29-Apr-16 14:38:54 PDT. 

可能是什麼造成的?我假設有人進入網站,代碼已經生成,然後他在38分鐘後點擊登錄,令牌已過期。我怎樣才能防止這一點?

我的代碼是這樣的:

$fbUser = $facebook->getUser(); 

if ($fbUser) 
{ 
    try 
    { 
     // We're logged in! 
     $user_profile = $facebook->api('/me'); 
    } 
    catch (FacebookApiException $e) 
    { 
     // Oh no, an error :(
     error_log($e); 
     $fbUser = null; 
    } 
} 

if ($fbUser) 
{ 
    try 
    { 
    $facebook_id = $facebook->getUser(); 
    $facebook_me = $facebook->api('/me'); 
    session->set('facebook_me', $facebook_me); 
    } 
    catch (FacebookApiException $e) 
    { 
    error_log($e); 
    } 
} 
else 
{ 
    $facebook_me = $session->value('facebook_me'); 
} 

回答

0
Please Used this code. This code is working in my web. 
    $fbconfig['appid' ] = "your_app_id"; 
    $fbconfig['secret'] = "your_secret_key"; 
    $fbconfig['baseurl'] = "your_redirect_url"; 

    $user     = null; //facebook user uid  
    // Create our Application instance. 
    $facebook = new Facebook(array('appId' => $fbconfig['appid'],'secret' => $fbconfig['secret'],'cookie' => true,)); 
    //Facebook Authentication part 
    $user  = $facebook->getUser(); 
    // We may or may not have this data based 
    $loginUrl = $facebook->getLoginUrl (
     array (
      'scope'   => 'email,user_birthday,user_location,user_work_history,user_about_me,user_hometown', 
      'redirect_uri' => $fbconfig['baseurl'] 
     ) 
    ); 
    $logoutUrl = $facebook->getLogoutUrl(); 
    if ($user) { 
     try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $user_profile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture'); 
      echo "<pre>";print_r($user_profile);die; 
     } catch (FacebookApiException $e) { 
     //you should use error_log($e); instead of printing the info on browser 
     d($e); // d is a debug function defined at the end of this file 
     $user = null; 
     } 
    } 
+0

謝謝!我會研究一下:) – user1996496

+0

我應該把'$ access_token = $ facebook-> getAccessToken();'在那裏嗎? – user1996496

+0

我認爲不需要訪問令牌,APPID和SECRET Key足以進行身份​​驗證。 –

相關問題