【问题标题】:How to check if video was watched by user through YouTube API如何检查用户是否通过 YouTube API 观看了视频
【发布时间】:2016-08-25 17:09:25
【问题描述】:

有什么方法可以检查用户是否通过 youtube API 观看了 youtube 上的某些视频?我不仅想知道用户是否点击了视频,还想知道他是否真的看过它。我基本上希望您在浏览 youtube 时可以在视频缩略图上看到那些显示已观看的信息。

【问题讨论】:

  • 发表你的尝试
  • 我在 youtube api 文档中没有找到任何会返回此类信息的 api 端点
  • 用户是否需要通过 Oauth 认证?这很重要
  • 用户通过 OAuth 进行身份验证

标签: youtube-api youtube-data-api youtube-javascript-api


【解决方案1】:

这是我的代码,我使用的是 javascript 和 youtube api v3 和 php。希望对你有帮助

`    protected function getHistory($vid){
    $return = new stdClass();
        $videos = $vid;
         $vids[] =$vid;

        $videos = explode("\n", $videos);       
        $videos = implode(",", array_map("rtrim", $videos));
                $part = "snippet,contentDetails,statistics";
        $this->url = "https://www.googleapis.com/youtube/v3/videos?key=    {$this->key}&part={$part}&id={$videos}";
                $data = $this->http(TRUE);
        $html ="<h2 class='header'>Your Recently Viewed Videos</h2> <a href='#' class='header' onclick='clearhistory();'>Clear History</a>";
        $html .= "<ul class='media' id='mediacontent'>";
        $vids = array();


        foreach ($data->items as $yt) {
        $html .= $this->listhisVideo($yt);
        $vids[] = $yt->id;
    }
    $html .= "</ul>";       
    $html .=$this->ads(728);



    $html .= '<script type="text/javascript">
        var videos = ["'.implode('","',$vids).'"];
    </script>';

    if(!isset($_POST['his'])) {

    $html ="<h2 class='header'>Your Recently Viewed Videos</h2>";
    $html .='<ul class="media"><li class="video" id="video-EoCz3Vx1pXg">

<center><img src="/img/load.gif"></center>

</li></ul>';
    $html .= '<form id="history" action="/history" method="POST">
<input type="hidden" id= "his" name="his"       value="NqucyQdiFRM,EoCz3Vx1pXg,FJ55SHCzt88" />

</form><script type="text/javascript">
if (localStorage.getItem("videos") === null) {
document.getElementById("his").value = "empty";  
} else {
document.getElementById("his").value = localStorage.getItem("videos");


}

document.getElementById("history").submit();
</script>';





    } 

    if ($_POST['his']=='empty'){
    $html ="<h2 class='header'>Your Recently Viewed Videos</h2>";
    $html .="NO Video Exist";

    }


    $return->html = $html;
    return $return;
}`

【讨论】:

  • 你能解释一下这段代码吗?我可以看到您正在向 /youue/v3/videos 发出请求,但这并不能告诉您用户是否观看了该视频。
  • 同意@Dejvid(并且不赞成):这列出了通过$vid 传递的视频的详细信息,但没有说明用户最近观看的内容。通过当前 API 似乎无法实现“最近观看”。
猜你喜欢
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 2022-10-06
相关资源
最近更新 更多