YouTube API - 如何获取过去30天的频道观看次数

时间:2016-09-02 18:42:33

标签: php laravel youtube youtube-api youtube-data-api

所以在阅读了youtube api的文档之后,我已经成功地在我的服务器端应用程序上为youtube帐户建立了oauth连接,但是我找不到如何通过使用我获得的数据来获取通道视图的解决方案。 所以我的代码看起来像这样(PHP部分):

class YouTube {

    protected $token;

    protected $params = [];

    /**
     * YouTube constructor.
     *
     * @param null $id
     */
    public function __construct($id = null)
    {
        if ($id) {
            $this->token = $this->getToken($id);
        }
    }

    /**
     * Get api authorization
     *
     * @return string
     */
    public function getAuthorizationUrl()
    {
        // Make redirect
        $this->params = [
            'client_id'    => '########',
            'redirect_uri' => '######',
            'scope'        => 'https://gdata.youtube.com&',
            'response_type'=> 'code&',
            'access_type'  => 'offline'
        ];
        $redirect_url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($this->params);

        return $redirect_url;
    }

    /**
     * Get API token and save account list to db
     *
     * @param $code
     *
     * @return \App\Models\DynamicDashboard\ThirdPartyAccounts
     */
    public function getCallbackUrl($code)
    {
        // Grab the returned code and extract the access token.
        $this->params = [
            'code'          => $code,
            'client_id'     => '####',
            'client_secret' => '#####',
            'redirect_uri'  => '#######',
            'grant_type'    => 'authorization_code'
        ];

        // Get access token
        $command = 'curl --data "' . http_build_query($this->params) . '" https://accounts.google.com/o/oauth2/token';
        exec($command, $resultToken);
        $resultToken = json_decode($resultToken[0]);

        // Do a request using the access token to get the list of accounts.
        $command = 'curl -H "Authorization: Bearer ' . $resultToken->access_token . '" https://accounts.google.com/o/oauth2.json';
        exec($command, $result);
        $result = json_decode($result[0]);

        // Save data to db
        $account = new ThirdPartyAccounts();
        $account->account_id = $result->identity->id;
        $account->type = 'youtube';
        $account->name = $result->identity->first_name . ' ' . $result->identity->last_name;
        $account->extra_details = json_encode([
            'access_token'  => $resultToken->access_token,
            'token_type'     => $resultToken->token_type,
            'expires_in'    => $resultToken->expires_in,
            'refresh_token' => $resultToken->refresh_token
        ]);

        $account->save();

        return $account;
    }

    /**
     * Get API token
     *
     * @param $id
     *
     * @return mixed
     */
    private function getToken($id)
    {
        $mainAccount = ThirdPartyAccounts::find($id);
        $youtubeToken = json_decode($mainAccount->extra_details);

        return $youtubeToken;
    }
}

但我不知道如何使用这些数据,我不知道如何编写过去30天获取频道观看所需的方法(每天都有数据)。 对不起,但我真的不太了解api连接,以便了解如何做到这一点,如果你愿意向我解释做什么,任何帮助都会受到欢迎。提前感谢大家的时间!

1 个答案:

答案 0 :(得分:0)

您可以使用reports.query方法执行此操作。它可让您检索许多不同的Google Analytics报告。每个请求使用查询参数来指定频道ID或内容所有者,开始日期,结束日期和至少一个度量。您还可以提供其他查询参数,例如维度,过滤器或排序说明。

在提供的链接中有一个示例可以检索最近30天,但它是用App Script编写的:

var analyticsResponse = YouTubeAnalytics.Reports.query(
'channel==' + channelId,
oneMonthAgoFormatted,
todayFormatted,
'views,likes,dislikes,shares',
{
dimensions: 'day',
sort: '-day'
});

要在PHP中执行此操作,我建议查看Youtube PHP code samples。它充满了指导和指示。

相关问题