运行 YouTube 配额不足,我有哪些选择?
Running out of YouTube quota what are my options?
我在客户关系部门工作并开发了一个应用程序来回复每条 YouTube 评论。
所以我现在正在做的基本上是一个脚本,它从 YouTube 数据 API v3 中提取评论数据。该脚本是一个循环脚本,每 30 秒调用一次,但是您可能知道 YouTube API 有配额限制,我一直在努力。
我愿意接受任何解决方案:
- 我是否需要向 YouTube 申请更多配额
- 我应该申请多少配额(基本上我只是从 who、id 和 timestamp 中提取评论数据)或者其他任何方式。
我的代码
<?php
if (!file_exists(__DIR__ . '/youtube_vendor/autoload.php')) {
throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/youtube_vendor/autoload.php';
include "mysql.php";
$db = new db();
$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope('https://www.googleapis.com/auth/youtube.readonly');
$client->addScope('https://www.googleapis.com/auth/youtube.force-ssl');
$client->setRedirectUri('your_url');
// offline access will give you both an access and refresh token so that
// your app can refresh the access token without user interaction.
$client->setAccessType('offline');
// Using "consent" ensures that your application always receives a refresh token.
// If you are not using offline access, you can omit this.
$client->setPrompt("consent");
$client->setIncludeGrantedScopes(true); // incremental auth
$auth_url = $client->createAuthUrl();
if(isset($_GET['code'])) {
// id index exists
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
// var_dump($access_token);
// echo "<br><br>";
// file_put_contents("received.txt",var_dump($access_token));
// $access_token = file_get_contents("received.txt");
// // $file = json_decode($fb);
// var_dump($access_token);
// serialize your input array (say $array)
$serializedData = serialize($access_token);
// save serialized data in a text file
file_put_contents('youtube_access_token.txt', $serializedData);
// at a later point, you can convert it back to array like:
$recoveredData = file_get_contents('youtube_access_token.txt');
// unserializing to get actual array
$access_token = unserialize($recoveredData);
// you can print your array like
print_r($access_token);
echo "<br>";
$client->setAccessToken($access_token);
$service = new Google_Service_YouTube($client);
// $channel = $youtube->channels->listChannels('snippet', array('mine' => $mine));
// var_dump($channel);
$queryParams = [
'maxResults' => 25,
'mine' => true
];
$arrayComment = array();
$arrayReplies = array();
$responseVideo = $service->activities->listActivities('snippet,contentDetails', $queryParams);
foreach($responseVideo['items'] as $video)
{
$db->insert_youtube_video($video['snippet']['channelId'],$video['snippet']['channelTitle'],$video['snippet']['publishedAt'],$video['snippet']['title'],$video['snippet']['description'],$video['snippet']['thumbnails']['standard']['url'],$video['contentDetails']['upload']['videoId']);
$queryParams = [
'videoId' => $video['contentDetails']['upload']['videoId']
];
$responseComment = $service->commentThreads->listCommentThreads('snippet,replies', $queryParams);
foreach($responseComment['items'] as $comment)
{
$db->insert_youtube_comment($comment['snippet']['topLevelComment']['snippet']['authorChannelUrl'],$comment['snippet']['topLevelComment']['snippet']['authorDisplayName'],$comment['snippet']['topLevelComment']['snippet']['authorProfileImageUrl'],$comment['snippet']['topLevelComment']['snippet']['publishedAt'],$comment['snippet']['topLevelComment']['snippet']['updatedAt'],$comment['snippet']['topLevelComment']['snippet']['textDisplay'],$comment['snippet']['topLevelComment']['snippet']['videoId'],$comment['snippet']['topLevelComment']['id']);
$queryParams = [
'parentId' => $comment['snippet']['topLevelComment']['id']
];
$responseReplies = $service->comments->listComments('snippet', $queryParams);
foreach ($responseReplies['items'] as $replies)
{
$db->insert_youtube_replies($replies['snippet']['authorChannelUrl'],$replies['snippet']['authorDisplayName'],$replies['snippet']['authorProfileImageUrl'],$replies['snippet']['publishedAt'],$replies['snippet']['updatedAt'],$replies['snippet']['textDisplay'],$replies['snippet']['videoId'],$comment['snippet']['topLevelComment']['id'],$replies['id']);
}
$arrayReplies[] = $responseReplies;
}
$arrayComment[] = $responseComment;
}
}
else
{
echo $auth_url;
}
?>
<textarea style="width:100%;height:300px"><?php print_r($responseVideo['items']); ?><?php print_r($arrayComment); ?><?php print_r($arrayReplies); ?></textarea>
<script>
setTimeout(function () { window.location.reload(); }, 15*1000);
document.write(new Date());
</script>
Do I have to apply for more quota to YouTube
如果您想避免达到配额并继续发出您现在发出的请求数。是的,您需要申请配额延期。
我会尽快申请延期可能需要很长时间。
How much quota should i apply ( basically im only pulling comment data, from who, id , and timestamp ) or sis there any other way.
这取决于你。您每天提出多少请求。您将需要做一些数学运算然后填充它以确保将来增加您的应用程序。
我在客户关系部门工作并开发了一个应用程序来回复每条 YouTube 评论。
所以我现在正在做的基本上是一个脚本,它从 YouTube 数据 API v3 中提取评论数据。该脚本是一个循环脚本,每 30 秒调用一次,但是您可能知道 YouTube API 有配额限制,我一直在努力。
我愿意接受任何解决方案:
- 我是否需要向 YouTube 申请更多配额
- 我应该申请多少配额(基本上我只是从 who、id 和 timestamp 中提取评论数据)或者其他任何方式。
我的代码
<?php
if (!file_exists(__DIR__ . '/youtube_vendor/autoload.php')) {
throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/youtube_vendor/autoload.php';
include "mysql.php";
$db = new db();
$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope('https://www.googleapis.com/auth/youtube.readonly');
$client->addScope('https://www.googleapis.com/auth/youtube.force-ssl');
$client->setRedirectUri('your_url');
// offline access will give you both an access and refresh token so that
// your app can refresh the access token without user interaction.
$client->setAccessType('offline');
// Using "consent" ensures that your application always receives a refresh token.
// If you are not using offline access, you can omit this.
$client->setPrompt("consent");
$client->setIncludeGrantedScopes(true); // incremental auth
$auth_url = $client->createAuthUrl();
if(isset($_GET['code'])) {
// id index exists
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
// var_dump($access_token);
// echo "<br><br>";
// file_put_contents("received.txt",var_dump($access_token));
// $access_token = file_get_contents("received.txt");
// // $file = json_decode($fb);
// var_dump($access_token);
// serialize your input array (say $array)
$serializedData = serialize($access_token);
// save serialized data in a text file
file_put_contents('youtube_access_token.txt', $serializedData);
// at a later point, you can convert it back to array like:
$recoveredData = file_get_contents('youtube_access_token.txt');
// unserializing to get actual array
$access_token = unserialize($recoveredData);
// you can print your array like
print_r($access_token);
echo "<br>";
$client->setAccessToken($access_token);
$service = new Google_Service_YouTube($client);
// $channel = $youtube->channels->listChannels('snippet', array('mine' => $mine));
// var_dump($channel);
$queryParams = [
'maxResults' => 25,
'mine' => true
];
$arrayComment = array();
$arrayReplies = array();
$responseVideo = $service->activities->listActivities('snippet,contentDetails', $queryParams);
foreach($responseVideo['items'] as $video)
{
$db->insert_youtube_video($video['snippet']['channelId'],$video['snippet']['channelTitle'],$video['snippet']['publishedAt'],$video['snippet']['title'],$video['snippet']['description'],$video['snippet']['thumbnails']['standard']['url'],$video['contentDetails']['upload']['videoId']);
$queryParams = [
'videoId' => $video['contentDetails']['upload']['videoId']
];
$responseComment = $service->commentThreads->listCommentThreads('snippet,replies', $queryParams);
foreach($responseComment['items'] as $comment)
{
$db->insert_youtube_comment($comment['snippet']['topLevelComment']['snippet']['authorChannelUrl'],$comment['snippet']['topLevelComment']['snippet']['authorDisplayName'],$comment['snippet']['topLevelComment']['snippet']['authorProfileImageUrl'],$comment['snippet']['topLevelComment']['snippet']['publishedAt'],$comment['snippet']['topLevelComment']['snippet']['updatedAt'],$comment['snippet']['topLevelComment']['snippet']['textDisplay'],$comment['snippet']['topLevelComment']['snippet']['videoId'],$comment['snippet']['topLevelComment']['id']);
$queryParams = [
'parentId' => $comment['snippet']['topLevelComment']['id']
];
$responseReplies = $service->comments->listComments('snippet', $queryParams);
foreach ($responseReplies['items'] as $replies)
{
$db->insert_youtube_replies($replies['snippet']['authorChannelUrl'],$replies['snippet']['authorDisplayName'],$replies['snippet']['authorProfileImageUrl'],$replies['snippet']['publishedAt'],$replies['snippet']['updatedAt'],$replies['snippet']['textDisplay'],$replies['snippet']['videoId'],$comment['snippet']['topLevelComment']['id'],$replies['id']);
}
$arrayReplies[] = $responseReplies;
}
$arrayComment[] = $responseComment;
}
}
else
{
echo $auth_url;
}
?>
<textarea style="width:100%;height:300px"><?php print_r($responseVideo['items']); ?><?php print_r($arrayComment); ?><?php print_r($arrayReplies); ?></textarea>
<script>
setTimeout(function () { window.location.reload(); }, 15*1000);
document.write(new Date());
</script>
Do I have to apply for more quota to YouTube
如果您想避免达到配额并继续发出您现在发出的请求数。是的,您需要申请配额延期。
我会尽快申请延期可能需要很长时间。
How much quota should i apply ( basically im only pulling comment data, from who, id , and timestamp ) or sis there any other way.
这取决于你。您每天提出多少请求。您将需要做一些数学运算然后填充它以确保将来增加您的应用程序。