/**
  * Add user auth link or process incoming auth requests.
  * @param array $options Plugin options array
  */
 protected function setUpFoursquareInteractions(array $options)
 {
     // Get the client ID and secret
     $client_id = $options['foursquare_client_id']->option_value;
     $client_secret = $options['foursquare_client_secret']->option_value;
     // Set up the redirect URL
     // Get a new configuration instance
     $config = Config::getInstance();
     // Get the root path of our install
     $site_root_path = $config->getValue('site_root_path');
     // If the server supports ssl add an s to our URL path
     $ssl = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != '' ? 's' : '';
     // Generate the redirect URL
     $redirect_uri = urlencode('http' . $ssl . '://' . $_SERVER['SERVER_NAME'] . $site_root_path . 'account/?p=foursquare');
     // Create the OAuth link based on foursquares instructions here: https://developer.foursquare.com/overview/auth
     $oauth_link = "https://foursquare.com/oauth2/authenticate?client_id=" . $client_id . "&response_type=code";
     $oauth_link .= "&redirect_uri=" . $redirect_uri;
     // Add the link for the user to click to the page
     $this->addToView('oauth_link', $oauth_link);
     // If we are here because they have been redirect back by foursquare with a OAuth token
     if (isset($_GET['code'])) {
         // Get the code foursquare provided from the URL
         $code = $_GET['code'];
         // Create a new crawler, as this class contains the method for retriving our tokens
         $crawler = new FoursquareCrawler(null, null);
         // Get the OAuth Tokens
         $tokens = $crawler->getOAuthTokens($client_id, $client_secret, $redirect_uri, $code);
         // If foursquare return an error
         if (isset($tokens->error)) {
             // Tell the user something went wrong
             $this->addErrorMessage("Oops! Something went wrong while obtaining OAuth tokens. foursquare says \"" . $tokens->error . ".\" Please double-check your settings and try again.", 'authorization');
         } else {
             // If we got some OAuth tokens back, check they are valid
             $foursquare_api_accessor = new FoursquareAPIAccessor();
             // Make a query for the users details on foursquare
             $foursquare_user = $foursquare_api_accessor->apiRequest('users/self', $tokens->access_token);
             // If foursquare returned an error after that request
             if (isset($foursquare_user->error) || !isset($foursquare_user->response->user->id) || !isset($foursquare_user->response->user->contact->email)) {
                 $this->addErrorMessage("Oops! Something went wrong querying the foursquare API.\n                         foursquare says \"" . Utils::varDumpToString($foursquare_user) . ".\" Please double-check your settings and try again.", 'authorization');
             } else {
                 // Everything went fine so store the details in the database
                 // Set the user ID and username based on details returned by foursquare
                 $foursquare_user_id = $foursquare_user->response->user->id;
                 $foursquare_username = $foursquare_user->response->user->contact->email;
                 // Save the tokens in the database
                 $this->saveAccessTokens($foursquare_user_id, $foursquare_username, $tokens->access_token);
             }
         }
     }
     // Create a new instance DAO
     $instance_dao = DAOFactory::getDAO('InstanceDAO');
     // Get the owner of this instance
     $owner_instances = $instance_dao->getByOwnerAndNetwork($this->owner, 'foursquare');
     // Add all owners of foursquare instances to the view
     $this->addToView('owner_instances', $owner_instances);
 }
Esempio n. 2
0
 public function testGetOAuthTokens()
 {
     // Create a new foursquare crawler for this instance with a valid access token
     $fsc = new FoursquareCrawler($this->profile1_instance, 'secret', 10);
     // Test getting token
     $tokens = $fsc->getOAuthTokens('ci', 'cs', 'http://test/account/?p=foursquare', '5dn');
     $this->assertEqual($tokens->access_token, 'secret');
 }