/**
  * Add user auth link or process incoming auth requests.
  * @param array $options Plugin options array
  */
 protected function setUpYouTubeInteractions(array $options)
 {
     //get options
     $client_id = $options['youtube_client_id']->option_value;
     $client_secret = $options['youtube_client_secret']->option_value;
     //prep redirect URI
     $config = Config::getInstance();
     $site_root_path = $config->getValue('site_root_path');
     $redirect_uri = urlencode(Utils::getApplicationURL() . 'account/?p=youtube');
     //create OAuth link
     $oauth_link = "https://accounts.google.com/o/oauth2/auth?client_id=" . $client_id . "&redirect_uri=" . $redirect_uri . "&scope=https://www.googleapis.com/auth/youtube.readonly%20https://www.googleapis.com/auth/plus.me" . "%20https://www.googleapis.com/auth/yt-analytics.readonly&response_type=code&access_type=offline" . "&approval_prompt=force";
     $this->addToView('oauth_link', $oauth_link);
     // Google provided a code to get an access token
     if (isset($_GET['code'])) {
         $code = $_GET['code'];
         $crawler_plugin_registrar = new YouTubeCrawler(null, null, null, null, null);
         $tokens = $crawler_plugin_registrar->getOAuthTokens($client_id, $client_secret, $code, 'authorization_code', $redirect_uri);
         if (isset($tokens->error)) {
             $this->addErrorMessage("Oops! Something went wrong while obtaining OAuth tokens.<br>YouTube says \"" . $tokens->error . ".\" Please double-check your settings and try again.", 'authorization');
         } else {
             if (isset($tokens->access_token)) {
                 // Get user data
                 // First we need to query the YouTube API for the users G+ ID
                 $youtube_api_accessor = new YouTubeAPIAccessor();
                 $fields = array("part" => "contentDetails", "mine" => "true");
                 $gplus_user_id_query = $youtube_api_accessor->apiRequest('channels', $tokens->access_token, $fields);
                 // The error we could get from this call is a forbidden error if something went wrong with
                 // authentication.
                 if (isset($gplus_user_id_query->error)) {
                     if ($gplus_user_id_query->error->code == "401" && $gplus_user_id_query->error->message == 'Unauthorized') {
                         $this->addErrorMessage("Oops! Looks like YouTube API access isn't turned on. " . "<a href=\"http://code.google.com/apis/console#access\">In the Google APIs console</a>, " . "in Services, flip the YouTube and YouTube analytics API Status switch to 'On' and try again\n                            .", 'authorization');
                     } else {
                         $this->addErrorMessage("Oops! Something went wrong querying the YouTube API.<br>" . "Google says \"" . $gplus_user_id_query->error->code . ": " . $gplus_user_id_query->error->message . ".\" Please double-check your settings and try again.", 'authorization');
                     }
                 } else {
                     // We have should have the users G+ id so we now just need their username from the G+ API
                     $gplus_id = $gplus_user_id_query->items[0]->contentDetails->googlePlusUserId;
                     $gplus_api_accessor = new GooglePlusAPIAccessor();
                     if (isset($gplus_id)) {
                         $gplus_user = $gplus_api_accessor->apiRequest('people/' . $gplus_id, $tokens->access_token, null);
                         if (isset($gplus_user->error)) {
                             if ($gplus_user->error->code == "403" && $gplus_user->error->message == 'Access Not Configured') {
                                 $this->addErrorMessage("Oops! Looks like Google+ API access isn't turned on. " . "<a href=\"http://code.google.com/apis/console#access\">In the Google APIs " . "console</a> in Services, flip the Google+ API Status switch to 'On' and " . "try again.", 'authorization');
                             } else {
                                 $this->addErrorMessage("Oops! Something went wrong querying the Google+ API.<br>" . "Google says \"" . $gplus_user->error->code . ": " . $gplus_user->error->message . ".\" Please double-check your settings and try again.", 'authorization');
                             }
                         } else {
                             if (isset($gplus_user->id) && isset($gplus_user->displayName)) {
                                 $gplus_user_id = $gplus_user->id;
                                 $gplus_username = $gplus_user->displayName;
                                 //Process tokens
                                 $this->saveAccessTokens($gplus_user_id, $gplus_username, $tokens->access_token, $tokens->refresh_token);
                             } else {
                                 $this->addErrorMessage("Oops! Something went wrong querying the Google+ API.<br>" . "Google says \"" . Utils::varDumpToString($gplus_user) . ".\" Please double-check your settings and try again.", 'authorization');
                             }
                         }
                     } else {
                         // It may be possible that the user has not linked their YouTube account to their G+ account
                         // so we might not get a G+ ID
                         $this->addErrorMessage("You don't have a Google+ ID associated with your YouTube account, " . "go to YouTube and link your Google+ account to your YouTube account to use this plugin. " . "For more information click <a href=https://www.thinkup.com/docs/userguide/settings/plugin" . "s/youtube.html>here</a>", 'authorization');
                     }
                 }
             }
         }
     }
     $instance_dao = DAOFactory::getDAO('InstanceDAO');
     $owner_instances = $instance_dao->getByOwnerAndNetwork($this->owner, 'youtube');
     $this->addToView('owner_instances', $owner_instances);
 }
Beispiel #2
0
 public function testGetOAuthTokensWithAndWithoutSSL()
 {
     $ytc = new YouTubeCrawler($this->instance, 'fauxaccesstoken', 10);
     //test getting token with HTTPS
     $_SERVER['SERVER_NAME'] = 'test';
     $_SERVER['HTTPS'] = 'y';
     $cfg = Config::getInstance();
     $cfg->setValue('site_root_path', '/');
     $redirect_uri = urlencode(Utils::getApplicationURL() . 'account/?p=youtube');
     $tokens = $ytc->getOAuthTokens('ci', 'cs', 'tc1', 'authorization_code', $redirect_uri);
     $this->assertEqual($tokens->access_token, 'faux-access-token-with-https');
     $this->assertEqual($tokens->refresh_token, 'faux-refresh-token-with-https');
     //test getting token without HTTPS
     $_SERVER['HTTPS'] = null;
     $redirect_uri = urlencode(Utils::getApplicationURL() . 'account/?p=youtube');
     $tokens = $ytc->getOAuthTokens('ci', 'cs', 'tc1', 'authorization_code', $redirect_uri);
     $this->assertEqual($tokens->access_token, 'faux-access-token-without-https');
     $this->assertEqual($tokens->refresh_token, 'faux-refresh-token-without-https');
 }