Пример #1
0
	public function callbackAction() {
		/* Get the saved tokens */
		$oauth_token = $this->_properties->getProperty('twitter_oauth_token');
		$oauth_token_secret = $this->_properties->getProperty('twitter_oauth_token_secret');
		
		if (!isset($oauth_token) && !isset($oauth_token_secret)) {
			$this->addErrorMessage("Missing temporary OAuth tokens");
			$this->_forward('index');
			return;			
		}
		
		/* Get the consumer key and secret from the config */
		if (! isset($this->_config->twitter->consumer_key) && !isset($this->_config->twitter->consumer_secret)) {
			$this->addErrorMessage("Missing OAuth consumer key and secret");
			$this->_forward('index');
			return;
		} 
		
		$consumer_key = $this->_config->twitter->consumer_key;
		$consumer_secret = $this->_config->twitter->consumer_secret;
		$oauth_callback = $this->getStaticUrl() . "/admin/sns/callback";
		
		/* If the oauth_token is old redirect to the connect page. */
		if (isset($_REQUEST['oauth_token'])) {
			if ($oauth_token != $_REQUEST['oauth_token']) {
				$this->_properties->deleteProperty("twitter_auth");
				die("Session should be cleared");
			}
		}
		
		/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
		$connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
		
		/* Request access tokens from twitter */
		$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
		
		/* Save the access tokens. Normally these would be saved in a database for future use. */
		$this->_properties->setProperty('twitter_oauth_token', $access_token['oauth_token']);
		$this->_properties->setProperty('twitter_oauth_token_secret', $access_token['oauth_token_secret']);
		$this->_properties->setProperty('twitter_user_id', $access_token['user_id']);
		$this->_properties->setProperty('twitter_username', $access_token['screen_name']);
		
		/* If HTTP response is 200 continue otherwise send to connect page to retry */
		if (200 == $connection->http_code) {
		  /* The user has been verified and the access tokens can be saved for future use */
		  $this->_properties->setProperty('twitter_auth', true);
		} else {
		  /* Save HTTP status for error dialog on connnect page.*/
		  die("Error, We should clear the session.");
		}
		
		$this->_forward('index');
	}