如何使用YouTube Analytics API v3获取youtube视频的分享数量?
我试图使用YouTube Analytics API v3获取youtube视频的份额。我正在使用谷歌PHP客户端库上传视频。我能找到的好恶等使用这个网址的计数:如何使用YouTube Analytics API v3获取youtube视频的分享数量?
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。
请亲引导我。谢谢。
我知道它已经晚了,但我仍然认为我应该分享这些代码,所以如果有人需要寻找类似于这样的功能的人,这将对他有所帮助。
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()));
}
请随时查看我的代码并提出任何合适的更改建议。
我在哪里可以获得此访问令牌?这是在文件'the_key.txt' – Rahul
@Rahul,检查这[链接](https://www.domsammut.com/code/php-server-side-youtube-v3-oauth-api-video-upload-指导)完整的教程.... –
我已经工作得很像。 我已经发布了一个新的问题在stackoverflow [这里](http://stackoverflow.com/questions/39505288/getting-forbidden-error-when-tying-to-excute-youtube-analytic-api) 您可以请回复同样。 – Rahul
YouTube Analytics API与您使用的YouTube数据API完全不同。 “分享指标”不公开,只有视频的所有者可以访问。文档:https://developers.google.com/youtube/analytics/v1/#Parameters – jkondratowicz
感谢您的回复,我想获取由我上传的视频的分享次数。 –
然后,您应该熟悉YT Analytics API和OAuth2流程。你要找的是'video'过滤器的“份额”指标。 – jkondratowicz