title: INSERT a comment to youtube by API PHP
body: <p>I am trying to insert a youtube video comment to a particular video but it keeps on redirecting and inserting my text many times to the video.please help me solve this problem and here's my code and I use <a href="https://github.com/google/google-api-php-client" rel="nofollow">google api library</a></p>
  
  <pre><code>require_once 'Google/autoload.php';
  require_once 'Google/Client.php';
  require_once 'Google/Service/YouTube.php';
  session_start();
  
  
  
  $OAUTH2_CLIENT_ID = 'XXX';
  $OAUTH2_CLIENT_SECRET = 'XXX';
  
  
  $VIDEO_ID = 'XXX';
  
  $TEXT = 'hi';
  
  $client = new Google_Client();
  $client-&gt;setClientId($OAUTH2_CLIENT_ID);
  $client-&gt;setClientSecret($OAUTH2_CLIENT_SECRET);
  $client-&gt;setScopes('https://www.googleapis.com/auth/youtube.force-ssl');
  $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],FILTER_SANITIZE_URL);
  $client-&gt;setRedirectUri($redirect);
  $authUrl = $client-&gt;createAuthUrl();
  header('Location: ' . $authUrl);
  $youtube = new Google_Service_YouTube($client);
      if (isset($_GET['code'])) {
          while(strval($_SESSION['state']) !== strval($_GET['state'])){
              $authUrl = $client-&gt;createAuthUrl();
              header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
              }
          $client-&gt;authenticate($_GET['code']);
          $_SESSION['token'] = $client-&gt;getAccessToken();
          header('Location: ' . $redirect);
      }
      if (isset($_SESSION['token'])) {
        $client-&gt;setAccessToken($_SESSION['token']);
      }
      if ($client-&gt;getAccessToken()) {
          try {
              $commentSnippet = new Google_Service_YouTube_CommentSnippet();
              $commentSnippet-&gt;setTextOriginal($TEXT);
              $topLevelComment = new Google_Service_YouTube_Comment();
              $topLevelComment-&gt;setSnippet($commentSnippet);
              $commentThreadSnippet = new Google_Service_YouTube_CommentThreadSnippet();
              $commentThreadSnippet-&gt;setTopLevelComment($topLevelComment);
              $commentThread = new Google_Service_YouTube_CommentThread();
              $commentThread-&gt;setSnippet($commentThreadSnippet);
              $commentThreadSnippet-&gt;setVideoId($VIDEO_ID);
              $videoCommentInsertResponse = $youtube-&gt;commentThreads-&gt;insert('snippet', $commentThread);
          } catch (Google_Service_Exception $e) {
              $htmlBody = sprintf('&lt;p&gt;A service error occurred: &lt;code&gt;%s&lt;/code&gt;&lt;/p&gt;',htmlspecialchars($e-&gt;getMessage()));
          } catch (Google_Exception $e) {
              $htmlBody = sprintf('&lt;p&gt;An client error occurred: &lt;code&gt;%s&lt;/code&gt;&lt;/p&gt;',htmlspecialchars($e-&gt;getMessage()));
          }
          $_SESSION['token'] = $client-&gt;getAccessToken();
  
      }
  </code></pre>