【问题标题】:INSERT a comment to youtube by API PHP通过 API PHP 向 youtube 插入评论
【发布时间】:2015-09-20 02:05:01
【问题描述】:

我正在尝试向特定视频插入 youtube 视频评论,但它不断将我的文本重定向并多次插入视频。请帮我解决这个问题,这是我的代码,我使用 google api library

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->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
$youtube = new Google_Service_YouTube($client);
    if (isset($_GET['code'])) {
        while(strval($_SESSION['state']) !== strval($_GET['state'])){
            $authUrl = $client->createAuthUrl();
            header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
            }
        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
        header('Location: ' . $redirect);
    }
    if (isset($_SESSION['token'])) {
      $client->setAccessToken($_SESSION['token']);
    }
    if ($client->getAccessToken()) {
        try {
            $commentSnippet = new Google_Service_YouTube_CommentSnippet();
            $commentSnippet->setTextOriginal($TEXT);
            $topLevelComment = new Google_Service_YouTube_Comment();
            $topLevelComment->setSnippet($commentSnippet);
            $commentThreadSnippet = new Google_Service_YouTube_CommentThreadSnippet();
            $commentThreadSnippet->setTopLevelComment($topLevelComment);
            $commentThread = new Google_Service_YouTube_CommentThread();
            $commentThread->setSnippet($commentThreadSnippet);
            $commentThreadSnippet->setVideoId($VIDEO_ID);
            $videoCommentInsertResponse = $youtube->commentThreads->insert('snippet', $commentThread);
        } 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();

    }

【问题讨论】:

  • 插入文本多少次?每次尝试插入文本的次数是否相同?
  • 它在做无限次

标签: php api youtube youtube-api youtube-data-api


【解决方案1】:

要使用 You Tube Data API 在 YouTube 视频中插入评论,应使用***commentThreads.insert*** 方法。我无法在您的代码中找到该方法。请查看此official documentation 以执行此操作。

希望有帮助!!

【讨论】:

  • 我使用的方法是 $videoCommentInsertResponse = $youtube->commentThreads->insert('sn-p', $commentThread);
猜你喜欢
  • 2015-08-23
  • 1970-01-01
  • 2017-11-29
  • 2016-09-05
  • 2014-10-05
  • 2018-10-28
  • 2019-08-11
  • 2020-01-03
  • 2016-08-27
相关资源
最近更新 更多