通过 API PHP 向 YouTube 插入评论
INSERT a comment to youtube by API PHP
我正在尝试向特定视频插入 YouTube 视频评论,但它不断重定向并多次插入我的文本 video.please 帮助我解决了这个问题,这是我的代码,我使用 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();
}
要使用 YouTube 数据 API 在 YouTube 视频中插入评论,应使用 ***commentThreads.insert***
方法。我无法在您的代码中找到该方法。请查看此 official documentation 以执行此操作。
希望对您有所帮助!!
我正在尝试向特定视频插入 YouTube 视频评论,但它不断重定向并多次插入我的文本 video.please 帮助我解决了这个问题,这是我的代码,我使用 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();
}
要使用 YouTube 数据 API 在 YouTube 视频中插入评论,应使用 ***commentThreads.insert***
方法。我无法在您的代码中找到该方法。请查看此 official documentation 以执行此操作。
希望对您有所帮助!!