我正在尝试使用YouTube Analytics API v3获取YouTube视频的分享数量。我正在使用谷歌php客户端库上传视频。我可以使用这个网址找到喜欢,不喜欢等的数量:
https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Nyk_ltlr6pc&key={APP_KEY}
但是无法找到任何此类资源来获取共享计数。
我看过这篇文章:How to get share count of a youtube video using youtube api?
但是我无法从这篇文章中获得任何内容,因为这篇文章已经过时了,并且正在使用YouTube Analytics API v1与当前的YouTube Analytics API v3进行比较。
请指导我。谢谢。
答案 0 :(得分:2)
我知道现在已经很晚了,但我认为我应该分享这些代码,所以如果有人需要寻找类似这样的功能,那会对他有所帮助。
set_include_path($_SERVER['DOCUMENT_ROOT'] . '/library/Youtube/src/');
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
$startdate = <DATE_WHEN_YOUR_GET_UPLOADED>;
$video_id = <YOUR_VIDEO_ID>;
$video_id = "video==" .$video_id;
$key = file_get_contents('the_key.txt'); //YOUR_ACCESS_TOKEN
$OAUTH2_CLIENT_ID = <YOUR_OAUTH2_CLIENT_ID>;
$OAUTH2_CLIENT_SECRET = <YOUR_OAUTH2_CLIENT_SECRET>;
$scope = array("https://www.googleapis.com/auth/youtube.force-ssl", "https://www.googleapis.com/auth/youtubepartner-channel-audit", "https://www.googleapis.com/auth/youtube", "https://www.googleapis.com/auth/youtube.readonly", "https://www.googleapis.com/auth/yt-analytics.readonly", "https://www.googleapis.com/auth/yt-analytics-monetary.readonly","https://www.googleapis.com/auth/youtubepartner");
try{
// Client init
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setAccessType('offline');
$client->setAccessToken($key);
$client->setScopes($scope);
if ($client->getAccessToken()) {
//Check to see if our access token has expired. If so, get a new one and save it to file for future use.
if($client->isAccessTokenExpired()) {
//refresh your access token if it's expired
$newToken = json_decode($client->getAccessToken());
$client->refreshToken($newToken->refresh_token);
file_put_contents('the_key.txt', $client->getAccessToken());
}
$analytics = new Google_Service_YouTubeAnalytics($client);
$channel_id = <YOUR_CHANNEL_ID>;
$ids = 'channel=='.$channel_id;
$end_date = date("Y-m-d"); //current date
$start_date = startdate; //date when you uploaded your video
$optparams = array(
'filters' => $video_id,
);
$metric = 'likes,shares'; //what you want to fetch
$api = $analytics->reports->query($ids, $start_date, $end_date, $metric, $optparams);
print_r(json_encode($api->rows[0]));
} else{
// @TODO Log error
echo 'Problems creating the client';
}
}catch (Google_Service_Exception $e) {
echo sprintf('<p>A service error occurred: <code>%s</code></p>',htmlspecialchars($e->getMessage()));
}
请随时查看我的代码并提出任何适当的更改建议。