如何使用 YouTube Analytics API v3 获取 youtube 视频的共享计数? [英] How to fetch share count of youtube video using YouTube Analytics API v3?

查看:35
本文介绍了如何使用 YouTube Analytics API v3 获取 youtube 视频的共享计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 YouTube Analytics API v3 获取 youtube 视频的分享次数.我正在使用 google php 客户端库上传视频.我可以使用此网址找到喜欢、不喜欢等的计数:

I m trying to fetch share count of youtube video using YouTube Analytics API v3. I am using google php client library to upload videos. I am able to find count of likes, dislikes, etc using this url:

https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Nyk_ltlr6pc&key={APP_KEY}

但无法找到任何此类资源来获取共享数.

But not able to find any such resource to fetch count of share.

我看过这个帖子:如何使用 youtube api 获取 youtube 视频的分享次数?

但是我无法从这篇文章中得到任何信息,因为这篇文章很旧,并且与当前的 YouTube Analytics API v3 相比,它使用的是 YouTube Analytics API v1.

But I am unable to get anything from this post, as this post is old and it is using YouTube Analytics API v1 compare to current YouTube Analytics API v3.

请指导我.谢谢.

推荐答案

我知道已经晚了,但我仍然认为我应该分享代码,所以如果有人需要寻找与此类似的功能,它会帮助他.

I know it's late, but still I think I should share the code, so if any need person looking for such functionality similar to this, it would help him.

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()));
}

请随时查看我的代码并提出任何合适的更改建议.

Kindly feel free to review my code and suggest any suitable changes.

这篇关于如何使用 YouTube Analytics API v3 获取 youtube 视频的共享计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆