gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:23:06 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com