将视频添加到 YouTube 上用户最喜欢/喜欢的播放列表

2024-02-01

目的是使用 YouTube API 创建一个收藏/喜欢按钮。当用户单击该按钮时,视频将保存到用户的“收藏/喜欢”播放列表中。

就像您在自己的网站上实施 Facebook Like 按钮时的工作原理一样。

这本质上是对发布的出色解决方案的后续问题伯特兰·马特尔 https://stackoverflow.com/a/42561941/1649673在我之前的question https://stackoverflow.com/q/42513339/1185126我们的目标是将视频添加到“稍后观看”播放列表中。

这个特定函数的工作代码是:

<!-- button 1 -->
<button type="submit" data-youtube-video-id="EH3gqI2NAiE" value="Watch Later" class="ma_youtube_watch_later" name="send">
  <div class="ma_youtube_watch_later_text">Watch Later</div>
</button>

<!-- button 2 -->
<button type="submit" data-youtube-video-id="0EMmKIIF-zE" value="Watch Later" class="ma_youtube_watch_later" name="send">
  <div class="ma_youtube_watch_later_text">Watch Later</div>
</button>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
  // By Bertrand Martel: https://stackoverflow.com/a/42561941/1649673
  var OAUTH2_CLIENT_ID = '28993181493-c9o6hdll3di0ssvebfd4atf13edqfu9g.apps.googleusercontent.com';
  var OAUTH2_SCOPES = [
    'https://www.googleapis.com/auth/youtube'
  ];
  var init = false;
  var youtube_video_id = '';

  var button = null;

  googleApiClientReady = function() {
    gapi.auth.init(function() {
      window.setTimeout(checkAuth, 1);
    });
  }

  function checkAuth() {
    gapi.auth.authorize({
      client_id: OAUTH2_CLIENT_ID,
      scope: OAUTH2_SCOPES,
      immediate: true
    }, handleAuthResult);
  }
  // Handle the result of a gapi.auth.authorize() call.
  function handleAuthResult(authResult) {

    jQuery('.ma_youtube_watch_later').off('click');
    jQuery('.ma_youtube_watch_later').click(function(e) {

      button = this;

      var youtube_video_id = jQuery(this).attr("data-youtube-video-id");

      // Add a video ID specified in the form to the playlist.
      function addVideoToPlaylist() {
        //addToPlaylist(jQuery('#video-id').val());
        addToPlaylist(youtube_video_id);
      }

      if (authResult && !authResult.error) {
        addVideoToPlaylist();
      } else {
        init = true;
        gapi.auth.authorize({
          client_id: OAUTH2_CLIENT_ID,
          scope: OAUTH2_SCOPES,
          immediate: false
        }, handleAuthResult);
      }
      return false;
    });

    if (authResult && !authResult.error) {
      // Authorization was successful. Hide authorization prompts and show
      // content that should be visible after authorization succeeds.
      jQuery('.pre-auth').hide();
      jQuery('.post-auth').show();
      loadAPIClientInterfaces();

      jQuery('#add_to_wl').click(function(e) {

        button = this;

        addVideoToPlaylist(self);
      });
    }
  }

  function loadAPIClientInterfaces() {
    gapi.client.load('youtube', 'v3', function() {
      if (init) {
        init = false;
        addVideoToPlaylist();
      }
    });
  }

  // Add a video to a playlist. The "startPos" and "endPos" values let you
  // start and stop the video at specific times when the video is played as
  // part of the playlist. However, these values are not set in this example.
  function addToPlaylist(id, startPos, endPos) {
    var details = {
      videoId: id,
      kind: 'youtube#video'
    }
    if (startPos != undefined) {
      details['startAt'] = startPos;
    }
    if (endPos != undefined) {
      details['endAt'] = endPos;
    }
    var request = gapi.client.youtube.playlistItems.insert({
      part: 'snippet',
      resource: {
        snippet: {
          playlistId: "WL",
          resourceId: details
        }
      }
    });

    request.execute(function(response) {
      console.log(response);
      if (!response.code) {
        //jQuery('#status').html('<pre>Succesfully added the video : ' + JSON.stringify(response.result) + '</pre>');

        // change button text
        $(button).text('Video added');

      } else if (response.code == 409) {

        //jQuery('#status').html('<p>Conflict : this video is already on your Watch Later playlist</p>');

        // change button text
        $(button).text('Already added');

      } else if (response.code == 404) {
        //jQuery('#status').html('<p>Not Found : this video hasnt been found</p>');

        // change button text
        $(button).text('Video not found');

      } else {
        //jQuery('#status').html('<p>Error : code ' + response.code + '</p>');

        // change button text
        $(button).text('Error: Try again');

      }
    });
  }

</script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>

并且我们有很多PHP 代码示例 https://developers.google.com/youtube/v3/code_samples/php在 API 文档上。我们还有一些文档将视频添加到播放列表 https://developers.google.com/youtube/2.0/developers_guide_protocol_playlists#Adding_a_video_to_a_playlist.

如何使用 PHP 或/和 javascript 来实现这一点?


您可以使用以下参数通过频道列表端点找到喜欢和收藏的播放列表 ID(检查) :

{
    mine: true,
    part: 'contentDetails'
}

您将获得播放列表列表relatedPlaylists包括likes, favorites, uploads, watch later and watch history.

{
    "kind": "youtube#channelListResponse",
    "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/S1x68O9aSpvmndklrnSwKw_yYdE\"",
    "pageInfo": {
        "totalResults": 1,
        "resultsPerPage": 1
    },
    "items": [{
        "kind": "youtube#channel",
        "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/ura_vsrPt5tCZkjjGbH3ihN3Bq4\"",
        "id": "UCnQt5EmYRfX1uVYtwPNj7Dg",
        "contentDetails": {
            "relatedPlaylists": {
                "likes": "LLnQt5EmYRfX1uVYtwPNj7Dg",
                "favorites": "FLnQt5EmYRfX1uVYtwPNj7Dg",
                "uploads": "UUnQt5EmYRfX1uVYtwPNj7Dg",
                "watchHistory": "HL",
                "watchLater": "WL"
            }
        }
    }]
}

对于 WatchLater 和 WatchHistory,所有用户的 playlistId 都相同(分别为 WL 和 HL)

您可以使用 Javascript 调用频道列表端点gapi.client.youtube.channels.list与调用播放列表插入 API 的方式相同。

然后,使用 playlistId,您可以将视频插入到“稍后观看”之外的另一个播放列表中。

在下面的代码中,您将找到 3 个按钮来添加不同的视频Watch Later播放列表,Like播放列表&Favorite播放列表


JavaScript

Here https://bertrandmartel.github.io/youtube-watch-later/multi-playlist.html是一个现场演示源代码 https://github.com/bertrandmartel/youtube-watch-later(如下)

Here https://jsfiddle.net/bmartel/3nsevqkx/4/是一把小提琴。替换您的客户端 ID 并在开发者控制台中添加为授权 JavaScript 来源:https://fiddle.jshell.net https://fiddle.jshell.net

<!doctype html>
<html>

<head>
    <title>Add to multiple playlists</title>
</head>

<body>
    <input type="submit" data-youtube-video-id="EH3gqI2NAiE" data-type="WL" value="Add to Watch Later playlist" class="yt_add_to_playlist" />
    <input type="submit" data-youtube-video-id="0EMmKIIF-z" data-type="likes" value="Add to Like playlist" class="yt_add_to_playlist" />
    <input type="submit" data-youtube-video-id="T4ZE2KtoFzs" data-type="favorites" value="Add to Favorite playlist" class="yt_add_to_playlist" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script>
    var OAUTH2_CLIENT_ID = '28993181493-c9o6hdll3di0ssvebfd4atf13edqfu9g.apps.googleusercontent.com';
    var OAUTH2_SCOPES = [
        'https://www.googleapis.com/auth/youtube'
    ];
    var init = false;
    var button = null;

    googleApiClientReady = function() {
        gapi.auth.init(function() {
            window.setTimeout(checkAuth, 1);
        });
    }

    function checkAuth() {
        gapi.auth.authorize({
            client_id: OAUTH2_CLIENT_ID,
            scope: OAUTH2_SCOPES,
            immediate: true
        }, handleAuthResult);
    }

    // Handle the result of a gapi.auth.authorize() call.
    function handleAuthResult(authResult) {

        jQuery('.yt_add_to_playlist').off('click');
        jQuery('.yt_add_to_playlist').click(function(e) {

            button = this;

            if (authResult && !authResult.error) {
                addToPlaylist($(this).attr("data-youtube-video-id"), $(this).attr("data-type"));
            } else {
                init = true;
                gapi.auth.authorize({
                    client_id: OAUTH2_CLIENT_ID,
                    scope: OAUTH2_SCOPES,
                    immediate: false
                }, handleAuthResult);
            }
            return false;
        });

        if (authResult && !authResult.error) {
            // Authorization was successful. Hide authorization prompts and show
            // content that should be visible after authorization succeeds.
            jQuery('.pre-auth').hide();
            jQuery('.post-auth').show();
            loadAPIClientInterfaces();

            jQuery('#add_to_wl').click(function(e) {
                button = this;
                addToPlaylist($(this).attr("data-youtube-video-id"), $(this).attr("data-type"));
            });
        }
    }

    function loadAPIClientInterfaces() {
        gapi.client.load('youtube', 'v3', function() {
            if (init) {
                init = false;
                addToPlaylist($(button).attr("data-youtube-video-id"), $(button).attr("data-type"));
            }
        });
    }

    function addToPlaylist(videoId, playlistType) {

        console.log("add to playlist type : " + playlistType);

        if (playlistType != "WL" && playlistType != "HL") {

            var request = gapi.client.youtube.channels.list({
                mine: true,
                part: 'contentDetails'
            });

            request.execute(function(response) {
                var playlistId = response.result.items[0].contentDetails.relatedPlaylists[playlistType];

                if (typeof playlistId != 'undefined') {
                    console.log("found playlist id : " + playlistId);
                    insertToPlaylist(videoId, playlistId);
                } else {
                    console.log("playlist not found");
                }

            });

        } else {
            insertToPlaylist(videoId, playlistType);
        }
    }

    function insertToPlaylist(videoId, playlistId) {

        var details = {
            videoId: videoId,
            kind: 'youtube#video'
        };
        var request = gapi.client.youtube.playlistItems.insert({
            part: 'snippet',
            resource: {
                snippet: {
                    playlistId: playlistId,
                    resourceId: details
                }
            }
        });

        request.execute(function(response) {
            console.log(response);
            if (!response.code) {
                $(button).val('Video added');
            } else if (response.code == 409) {
                $(button).val('Already added');
            } else if (response.code == 404) {
                $(button).val('Video not found');
            } else {
                $(button).val('Error: Try again');
            }
        });
    }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>

</html>

Replace OAUTH2_CLIENT_ID使用您自己的客户 ID

在 API 响应中,如果未找到视频,则会检查状态代码 (404). The 409如果视频已在播放列表中,则返回状态代码,但仅用于稍后观看播放列表(将现有视频添加到喜欢/最喜欢的播放列表不会改变任何内容)


PHP

与之前相同,基于谷歌 API PHP 示例 https://github.com/youtube/api-samples/blob/master/php/playlist_updates.php :

  • 安装作曲家:参见指示 https://getcomposer.org/download/
  • 安装 google-api 客户端:

    composer require google/apiclient:~2.0
    

PHP 脚本multi-playlist.php https://github.com/bertrandmartel/youtube-watch-later/blob/master/multi-playlist.php :

<?php
/**
 * Library Requirements
 *
 * 1. Install composer (https://getcomposer.org)
 * 2. On the command line, change to this directory (api-samples/php)
 * 3. Require the google/apiclient library
 *    $ composer require google/apiclient:~2.0
 */
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
  throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
}
require_once __DIR__ . '/vendor/autoload.php';
session_start();

$response = "";

/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
$OAUTH2_CLIENT_ID = 'YOUR_CLIENT_ID';
$OAUTH2_CLIENT_SECRET = 'YOUR_CLIENT_SECRET';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');

$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);

$client->setRedirectUri($redirect);
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
// Check if an auth token exists for the required scopes
$tokenSessionKey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }
  $client->authenticate($_GET['code']);
  $_SESSION[$tokenSessionKey] = $client->getAccessToken();
  header('Location: ' . $redirect);
}
if (isset($_SESSION[$tokenSessionKey])) {
  $client->setAccessToken($_SESSION[$tokenSessionKey]);
}
// Check to ensure that the access token was successfully acquired.

if ($client->getAccessToken()) {
  try {

    $videoId = "";

    if (isset($_GET['video'])){
      $videoId = $_GET['video'];
    }
    else if(isset($_SESSION['video'])){
      $videoId = $_SESSION['video'];
    }

    if (isset($_GET['action'])){
      $action = $_GET['action'];
    }
    else if(isset($_SESSION['action'])){
      $action = $_SESSION['action'];
    }

    if(isset($videoId) && isset($action) && !isset($_GET['state'])) {

      file_put_contents('php://stderr', print_r("adding video to watch later playlist " . $videoId . "\n", TRUE));

      if ($action == "Add to Watch Later playlist") {
        $playlistId = "WL";
      }
      else {
          $listResponse = $youtube->channels->listChannels("contentDetails", array(
              'mine' => true
          ));

          if (!empty($listResponse)) {
            if ($action == "Add to Like playlist"){
              $playlistId = $listResponse['items'][0]['contentDetails']['relatedPlaylists']['likes'];
            }
            else if ($action == "Add to Favorite playlist"){
              $playlistId = $listResponse['items'][0]['contentDetails']['relatedPlaylists']['favorites'];
            }
          }
      }

      if (isset($playlistId)){

        // 5. Add a video to the playlist. First, define the resource being added
        // to the playlist by setting its video ID and kind.
        $resourceId = new Google_Service_YouTube_ResourceId();
        $resourceId->setVideoId($videoId);
        $resourceId->setKind('youtube#video');

        // Then define a snippet for the playlist item. Set the playlist item's
        // title if you want to display a different value than the title of the
        // video being added. Add the resource ID and the playlist ID retrieved
        // in step 4 to the snippet as well.
        $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
        $playlistItemSnippet->setTitle('First video in the test playlist');
        $playlistItemSnippet->setPlaylistId($playlistId);
        $playlistItemSnippet->setResourceId($resourceId);
        // Finally, create a playlistItem resource and add the snippet to the
        // resource, then call the playlistItems.insert method to add the playlist
        // item.
        $playlistItem = new Google_Service_YouTube_PlaylistItem();
        $playlistItem->setSnippet($playlistItemSnippet);

        $playlistItemResponse = $youtube->playlistItems->insert(
            'snippet,contentDetails', $playlistItem, array());

        $response = json_encode($playlistItem);
      }
      else{
        $response = "no playlist selected";
      }

      $_SESSION['video'] = "";
      $_SESSION["action"] = "";
  }
  else{
    file_put_contents('php://stderr', print_r("no video was specified", TRUE));
  }

  } catch (Google_Service_Exception $e) {
    $response = htmlspecialchars($e->getMessage());
  } catch (Google_Exception $e) {
    $response = htmlspecialchars($e->getMessage());
  }
  $_SESSION[$tokenSessionKey] = $client->getAccessToken();
} else {

  if(isset($_GET['video'])){

    $_SESSION["video"] = $_GET['video'];
    $_SESSION["action"] = $_GET['action'];

    // If the user hasn't authorized the app, initiate the OAuth flow
    $state = mt_rand();
    $client->setState($state);
    $_SESSION['state'] = $state;
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl);
  }
}
?>

<!doctype html>
<html>
<head>
 <title>Add to playlists</title>
</head>
<body>
    <div>
        <form id="form" action="multi-playlist.php"">
            <input type="hidden"  name="video" value="EH3gqI2NAiE">
            <input name="action" type="submit" value="Add to Watch Later playlist" />
            <input name="action" type="submit" value="Add to Like playlist" />
            <input name="action" type="submit" value="Add to Favorite playlist" />
        </form>
        <div>
          <?php echo $response ?>
        </div>
    </div>
</body>
</html>

对于这个 PHP 版本,请注意除了当前会话中的视频 id$_SESSION["video"],目标动作也存储在$_SESSION["action"]能够确定要使用哪个播放列表。使用频道列表端点检索播放列表$youtube->channels->listChannels.


请注意,视频可能需要一些时间才会出现在播放列表中(有时需要几秒钟)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将视频添加到 YouTube 上用户最喜欢/喜欢的播放列表 的相关文章

随机推荐

  • 如何在 Eclipse 中调试来自 Maven 构建的测试?

    如何启动 Maven 构建并让测试出现在 Eclipse 的调试透视图中 包或安装是一个原子操作 实际上不应该停止调试 除非您正在调试 Maven 插件 不过 您应该能够通过选择 Eclipse 项目并执行 调试为 gt JUnit 测试
  • 如何单击 python-capmonster 中的确认按钮

    每个人 你好吗 现在我正在制作脚本来解决验证码问题 使用 python capmonster 模块 我可以获得响应 所以我输入了响应
  • 如何消除条形图中条形之间的间隙

    我正在 Matplotlib 中通过如下调用制作条形图 xs bar bar lefts bar heights facecolor black edgecolor black 我得到一个如下所示的条形图 我想要的是连续条之间没有白色间隙的
  • 当我按回车键或在文本字段外部单击时,如何删除文本字段焦点? (SwiftUI、MacOS)

    当我按回车键或在文本字段外部单击时 如何删除文本字段焦点 请注意 这是 SwiftUIMacOS 如果我这样做 import SwiftUI struct ContentView View State var field1 String T
  • Hive:选择 AS 和分组依据

    我有一个 Hive 查询 例如 SELECT Year Month Day Hours Minutes cast cast Seconds as int 15 as int 15 AS secondMod Count AS PerCount
  • XMLHttpRequest 返回错误编码的字符

    我使用 XMLHttpRequest 来读取 PDF 文档http www virtualmechanics com support tutorials spinner Simple2 pdf http www virtualmechani
  • 无法执行目标 org.apache.maven.plugins:maven-clean-plugin:2.4.1

    我正在做一个maven项目 我的IDE是Eclipse 我无法掌握的是当我在Eclipse中使用 clean X 时 它会报告以下错误 Apache Maven 3 0 4 r1232337 2012 01 17 16 44 56 0800
  • 将 Emacs AUCTeX 与 Sumatra PDF 同步

    有了我的这些行init el我能够将 Emacs LaTeX 缓冲区与 Sumatra 同步 setq TeX source correlate mode t setq TeX source correlate method synctex
  • 为什么我会出现 Keras 形状不匹配的情况?

    我正在遵循一个针对初学者的 Keras mnist 示例 我尝试更改标签以适合我自己的数据 该数据有 3 个不同的文本分类 我正在使用 to categorical 来实现这一点 形状对我来说看起来不错 但 fit 会出现错误 train
  • 查找数组中长度为 k 的所有子集

    给定一组 1 2 3 4 5 n 对于 n 个元素 我们需要找到长度为 k 的所有子集 例如 如果 n 4 且 k 2 则output将会 1 2 1 3 1 4 2 3 2 4 3 4 我什至不知道如何开始 我们不必使用内置的库函数 如
  • 你怎么会忘记 Laravel 中缓存的 Eloquent 模型呢?

    关于 Laravel 的理论问题在这里 所以我要做的缓存示例是 Article with comments gt remember 5 gt get 理想情况下 我希望有一个文章更新事件 当该模型的实例 已缓存 的 ID 更新时 我想忘记该
  • 存在哪些 ASP.NET MVC 示例项目? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 混合语言框架

    我有一个用 Objective C 编写的框架 我们称之为 MyKit 我用一些 Swift 类对其进行了扩展 我正在尝试使用以下文档来理解它 https developer apple com library ios documentat
  • OpenGL ES 片段着色器可以更改片段的深度值吗?

    OpenGL ES 2 0 中的片段着色器可以更改像素的 Z 值 深度 吗 在 OpenGL ES 2 0 中这是如何实现的 No gl FragDepth 这是 GLSL 桌面版本的一部分 在 OpenGL ES 中不存在 但是 您可以检
  • NHibernate 将类属性映射到行而不是列

    是否可以映射一个类 其中每个属性存储为表中的行而不是列 该场景是我们将全局选项保存到数据库的情况 我们将选项存储在 选项 类中 每个选项都有一个属性 即 展开菜单 退出时保存 等 我们不想将每个选项存储在其自己的表列中 而是希望有一个表 其
  • 如何获取包含占位符的属性的原始值?

    我试图在我的 properties 文件之一中定义以下属性 personExpression person surname 然后由配置类读取 Configuration public class TemplateConfig Autowir
  • 在 Chrome 扩展程序中获取 JSON

    我的 chrome 扩展有一个小问题 我只是想从另一台服务器获取 JSON 数组 但清单 2 不允许我这样做 我尝试指定content security policy 但 JSON 数组存储在没有 SSL 证书的服务器上 那么 如果不使用清
  • 更改 varchar 列的最大长度?

    我正在尝试将 varchar 列的长度从 255 个字符更新为 500 个字符 而不会丢失内容 我之前已经删除并重新创建过表 但我从未接触过 alter 语句 我相信我需要使用它来执行此操作 我在这里找到了文档 更改表 Transact S
  • Python——使用 Selenium 打开多个选项卡

    我正在使用Python 我正在尝试在 Chrome 上打开两个选项卡 每个选项卡都指向不同的网站 这是我的代码 from selenium import webdriver from selenium webdriver common ke
  • 将视频添加到 YouTube 上用户最喜欢/喜欢的播放列表

    目的是使用 YouTube API 创建一个收藏 喜欢按钮 当用户单击该按钮时 视频将保存到用户的 收藏 喜欢 播放列表中 就像您在自己的网站上实施 Facebook Like 按钮时的工作原理一样 这本质上是对发布的出色解决方案的后续问题