php - 通过API PHP向YouTube插入评论

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

我正在尝试向特定视频插入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();

    }

最佳答案

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

希望有帮助!!

关于php - 通过API PHP向YouTube插入评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31186332/

相关文章:

PHP如何使用html表格按钮从数据库中删除特定行

php - ISO-8859-1 字符截断插入到 utf-8 mysql 列中的文本

angularjs - CSRF token 丢失或不正确。 Django + AngularJS

reactjs - Next.js getStaticPaths

JavaFX 2 嵌入 YouTube 视频

java - 如何使用 Tomcat 8 在 Linux 上安装 Quercus

android - Google Vision API - 空对象引用上的人脸方法

iphone - 在您的 App Swift 代码上启动 Youtube channel

javascript - 如何为YouTube视频的手动输入的iframe捕获OnStateChange事件

php - MySQL 查询 - 按月和年查找范围内的日期