}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
    try {
        // This code subscribes the authenticated user to the specified channel.
        // Identify the resource being subscribed to by specifying its channel ID
        // and kind.
        $resourceId = new Google_Service_YouTube_ResourceId();
        $resourceId->setChannelId('UCtVd0c0tGXuTSbU5d8cSBUg');
        $resourceId->setKind('youtube#channel');
        // Create a snippet object and set its resource ID.
        $subscriptionSnippet = new Google_Service_YouTube_SubscriptionSnippet();
        $subscriptionSnippet->setResourceId($resourceId);
        // Create a subscription request that contains the snippet object.
        $subscription = new Google_Service_YouTube_Subscription();
        $subscription->setSnippet($subscriptionSnippet);
        // Execute the request and return an object containing information
        // about the new subscription.
        $subscriptionResponse = $youtube->subscriptions->insert('id,snippet', $subscription, array());
        $htmlBody .= "<h3>Subscription</h3><ul>";
        $htmlBody .= sprintf('<li>%s (%s)</li>', $subscriptionResponse['snippet']['title'], $subscriptionResponse['id']);
        $htmlBody .= '</ul>';
    } catch (Google_Service_Exception $e) {
        $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
    } catch (Google_Exception $e) {
        $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
    }
    $_SESSION['token'] = $client->getAccessToken();
} else {
    // If the user has not authorized the application, start the OAuth 2.0 flow.
    $state = mt_rand();
 function addChannelToSubscriptions($channel_id)
 {
     // include the required php files - containers client_id and client_secret
     include_once YT4WP_PATH . 'lib/google_api_wrapper_clientid_clientsecret.php';
     try {
         $resourceId = new Google_Service_YouTube_ResourceId();
         $resourceId->setChannelId($channel_id);
         $resourceId->setKind('youtube#channel');
         $subscriptionSnippet = new Google_Service_YouTube_SubscriptionSnippet();
         $subscriptionSnippet->setResourceId($resourceId);
         $subscription = new Google_Service_YouTube_Subscription();
         $subscription->setSnippet($subscriptionSnippet);
         $subscriptionResponse = $youtube->subscriptions->insert('id,snippet', $subscription, array());
         if ($subscriptionResponse) {
             // ajax to return our unsubscribe ID (which differs from the subscribe ID)
             // Check if the user is subscribed to this channel
             $is_user_subscribed = $youtube->subscriptions->listSubscriptions('id', array('mine' => 'true', 'forChannelId' => $channel_id, 'maxResults' => 1));
             if ($is_user_subscribed['items']) {
                 $unsubscribe_id = $is_user_subscribed['items'][0]['id'];
             }
             echo $unsubscribe_id;
         }
     } catch (Google_ServiceException $e) {
         echo '<span id="response_message" class="yt4wp-error-alert"><strong>Oh No!</strong> We have encountered a serious error : <code> ' . $e->getMessage() . '</code>' . $e->getCode();
         /* Write the error to our error log */
         $this->writeErrorToErrorLog($e->getMessage(), $e->getCode());
     } catch (Google_Exception $e) {
         echo '<span id="response_message" class="yt4wp-error-alert"><strong>Oh No!</strong> We have encountered a serious error : <code> ' . $e->getMessage() . '</code>' . $e->getCode();
         /* Write the error to our error log */
         $this->writeErrorToErrorLog($e->getMessage(), $e->getCode());
     }
 }