如何使用 YouTube 数据 API 将 YouTube 视频添加到观看记录? [英] How can I add a YouTube video to the Watch History with the YouTube Data API?

查看:27
本文介绍了如何使用 YouTube 数据 API 将 YouTube 视频添加到观看记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 YouTube Data API 3.0 制作一个网站,用户可以在其中观看 YouTube 视频.我需要将这些视频转到观看历史记录,以便我知道它们已被观看过,但使用 YouTube 播放器 API 播放视频不会添加它们.我已经使用 playlistItems.insert 将视频 ID 添加到用户的观看历史播放列表 ID 中,如果我访问 youtube.com,视频就会显示在观看历史播放列表中.但是,当我使用 Data API 请求播放列表项时,它不会在列表中返回.

I'm making a site with the YouTube Data API 3.0 where users can watch YouTube videos. I need these videos to go to the Watch History so I can know that they've been watched, but playing videos with the YouTube Player API doesn't add them. I've added the video ID to the user's watch history playlist id with playlistItems.insert and the video shows up on the Watch History playlist if I go to youtube.com. However, it isn't returned in the list when I request the playlist items with the Data API.

有谁知道如何将视频添加到观看历史记录中,以便在 API 调用中返回它们?或者,有没有办法使用播放器 API,以便经过身份验证的用户在其观看历史记录中获取视频?

Does anyone know how to add videos to the watch history so that they're returned in API calls? Alternatively, is there a way to use the Player API so that the authenticated user get the video on their watch history?

这是将视频添加到用户观看历史记录的代码:

Here is the code to add a video to the user's watch history:

gapi.client.youtube.playlistItems.insert({
  'part': 'snippet',
  'resource': {
    'snippet': {
      'playlistId': Cache.fetch('my_channel').relatedPlaylists.watchHistory,
      'position': 0,
      'resourceId': {
        'kind': 'youtube#video',
        'videoId': video_id,
      }
    }
  }
}).execute(callback);

推荐答案

您可以使用此代码通过 http 和 json 进行连接和请求

You can use this code for connect and request using http and json

try {

        URL url = new URL("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet"
                + "&key=AIzaSyAhONZJpMCBqCfQjFUj21cR2klf6JWbVSo"
                + "&access_token=" + access_token);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");

        String input = "{ \"snippet\": {\"playlistId\": \"WL\",\"resourceId\": {\"videoId\": \""+videoId+"\",\"kind\": \"youtube#video\"},\"position\": 0}}";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

     }

如果您看到错误,请添加此内容,但这不是一个好工作.

if you see an error add this but this is not a good job.

if (Build.VERSION.SDK_INT >= 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

这篇关于如何使用 YouTube 数据 API 将 YouTube 视频添加到观看记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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