$facebook->api(‘/me’)でException

An active access token must be used to query information about the current user.

というエラーが発生し、追っていくと
$facebook->api(‘/me’)でExceptionが発生しているようだった。

どうも、$facebook->getUser()が取れていないと起こるものらしい。
そのため、try catchやif elseで取れていない時は強制的にログインさせる実装に変更して解決させた。

ローカル環境だとできるが、WEBサーバだと出来ないのは未だに謎。

	public function authenticate(CakeRequest $request,CakeResponse $response){
		CakeLog::write('debug', '[FacebookAuthenticate/authenticate] REQUEST');
		try{
			$facebook = new Facebook(array(
				'appId'=>$this->settings['fbAppId'],
				'secret'=>$this->settings['fbAppSecret'],
				'trustForwarded' => true,
				'cookie'=>true
			));
		
			switch ($request->params['action']){
				case 'fblogin':
					CakeLog::write('debug', '[FacebookAuthenticate/authenticate login] REQUEST');					
					$login_url = $facebook->getLoginUrl(array(
						'redirect_uri'=>$this->settings['fbRedirect'],
						'scope'=>$this->settings['fbScope'],
					));
					$response->header('Location',$login_url);
					$response->send();
					break;
				case 'callback':
					CakeLog::write('debug', '[FacebookAuthenticate/authenticate callback] REQUEST');			
					$fbuser = $facebook->getUser();
					try{
						if($fbuser){
							$user_profile = $facebook->api('/me');
							return $this->_fb_update_user(
								$user_id = $facebook->getUser(),
								$facebook->getAccessToken(),
								$user_profile['email']
							);
							break;				
						}else{
							$login_url = $facebook->getLoginUrl(array(
								'redirect_uri'=>$this->settings['fbRedirect'],
								'scope'=>$this->settings['fbScope'],
							));
							$response->header('Location',$login_url);
							$response->send();
							break;
						}
					}catch(FacebookApiException $e){
						error_log($e->getType());
						error_log($e->getMessage());						
						$login_url = $facebook->getLoginUrl(array(
							'redirect_uri'=>$this->settings['fbRedirect'],
							'scope'=>$this->settings['fbScope'],
						));
						$response->header('Location',$login_url);
						$response->send();
						break;
					}
			}
		} catch(OAuthException $e){
			CakeLog::write('debug', '[FacebookAuthenticate/authenticate OAuthException] REQUEST');					
			debug($e);
		}
	}

参考記事

http://programming-10000.haten……1381190156
http://fb.dev-plus.jp/forum/to…….php?id=67