首页 文章

Youtube api v3获取用户视频列表

提问于
浏览
59

使用Youtube api v2,可以轻松获取视频 . 只需发送如下查询:

http://gdata.youtube.com/feeds/mobile/videos?max-results=5&alt=rss&orderby=published&author=OneDirectionVEVO

Youtube api v2还有一个用于构建查询的交互式演示页面:http://gdata.youtube.com/demo/index.html

有了Youtube api v3,我不知道相应的方式 . 请用api v3指出方向 .

谢谢!

13 回答

  • 0

    如果它能帮到这里的任何人,这就是我发现的东西,到目前为止似乎对我有用 . 我在发出此请求之前通过OAuth 2.0对该成员进行身份验证,这将为我提供经过身份验证的成员视频 . 与往常一样,您的个人里程可能会有所不同:D

    curl https://www.googleapis.com/youtube/v3/search -G \
    -d part=snippet \
    -d forMine=true \
    -d type=video \
    -d order=date \
    -d access_token={AUTHENTICATED_ACCESS_TOKEN}
    
  • 2

    channels#list方法将返回一个JSON,其中包含有关该 Channels 的一些信息,包括"uploads"播放列表的播放列表ID:

    https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=OneDirectionVEVO&key={YOUR_API_KEY}
    

    使用播放列表ID,您可以使用playlistItems#list方法获取视频:

    https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUbW18JZRgko_mOGm5er8Yzg&key={YOUR_API_KEY}
    

    您可以在文档页面的末尾测试它们 .

  • 0

    这应该做到这一点 . 此代码只获取并输出 Headers ,但您可以获得所需的任何详细信息

    // Get Uploads Playlist
    $.get(
       "https://www.googleapis.com/youtube/v3/channels",{
       part : 'contentDetails', 
       forUsername : 'USER_CHANNEL_NAME',
       key: 'YOUR_API_KEY'},
       function(data) {
          $.each( data.items, function( i, item ) {
              pid = item.contentDetails.relatedPlaylists.uploads;
              getVids(pid);
          });
      }
    );
    
    //Get Videos
    function getVids(pid){
        $.get(
            "https://www.googleapis.com/youtube/v3/playlistItems",{
            part : 'snippet', 
            maxResults : 20,
            playlistId : pid,
            key: 'YOUR_API_KEY'},
            function(data) {
                var results;
                $.each( data.items, function( i, item ) {
                    results = '<li>'+ item.snippet.title +'</li>';
                    $('#results').append(results);
                });
            }
        );
    }
    
    
    <!--In your HTML -->
    <ul id="results"></ul>
    
  • 20

    事情已经在API的V3中发生了很多变化 . 这是一个video,它引导您完成获取在给定 Channels 中上传的视频列表所需的v3 API调用,并使用API Explorer进行实时演示 .

    YouTube Developers Live: Getting a Channel's Uploads in v3 - https://www.youtube.com/watch?v=RjUlmco7v2M

  • 0

    您发布的请求的等效实际上是3.0 api中的搜索,而不是播放列表请求 . 这样做也更容易 . 您确实需要为 Channels ID提取用户名 .

    恩 . GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUGhCVGZ0ZSpe5hJHWyiLwHA&key=

  • 2

    如果考虑配额成本,遵循这个简单的算法可能是有益的 .

    首先从https://www.youtube.com/feeds/videos.xml?channel_id=获取数据......这是一个简单的XML提要,它将为您提供视频ID 's, but you cannot specify further ' parts'(统计数据等) .

    使用该列表中的视频ID,在/ videos API endpoints 上进行查询,该 endpoints 允许以逗号分隔的视频ID列表,这些列表只会产生1个配额成本,加上任何其他部分参数的0-2 . 正如@chrismacp指出的那样,使用/ search endpoints 更简单,但配额成本为100,可以快速加起来 .

    当你进行第二次调用时,这里有一个资源考虑(cpu,内存等),但我相信在许多情况下这可能是一个有用的方法 .

  • 0

    另一种方法可能是通过以下方式获取当前oauth认证用户的播放列表:property mine = true

    在验证后检索oauth access_token:https://developers.google.com/youtube/v3/guides/authentication

    https://www.googleapis.com/youtube/v3/playlists?part=id&mine=true&access_token=ya29.0gC7xyzxyzxyz
    
  • 4

    如果您想获取包含300多个视频的播放列表视频,请不要使用playlistitems.list . 你可以在谷歌链接“https://developers.google.com/youtube/v3/docs/playlistItems/list " in "试试吧”部分试用它 . 它返回undefined .

    我也在我的项目中使用过 . 它仅返回undefined .

  • 0

    在PHP:我使用pageToken属性转到播放列表的所有页面 . 我希望它可以帮助你 .

    //step 1: get playlist id
    
     $response = file_get_contents("https://www.googleapis.com/youtube/v3/channels?key={$api_key}&forUsername={$channelName}&part=contentDetails");
     $searchResponse = json_decode($response,true);
     $data = $searchResponse['items'];
     $pid =  $data[0]['contentDetails']['relatedPlaylists']['uploads'];
    
    //step 2: get all videos in playlist
    
     $nextPageToken = '';
     while(!is_null($nextPageToken)) {
         $request = "https://www.googleapis.com/youtube/v3/playlistItems?key={$api_key}&playlistId={$pid}&part=snippet&maxResults=50&pageToken=$nextPageToken";
    
        $response = file_get_contents($request);
        $videos = json_decode($response,true);
    
        //get info each video here...
    
       //go next page
        $nextPageToken = $videos['nextPageToken'];
    }
    
  • 2

    以下是一些使用官方Google API节点库的代码(https://github.com/google/google-api-nodejs-client

    const readJson = require("r-json");
    const google = require('googleapis');
    const Youtube = google.youtube('v3');
    
    // DONT store your credentials in version control
    const CREDENTIALS = readJson("/some/directory/credentials.json");
    
    let user = "<youruser>";
    let numberItems = 10; 
    
    let channelConfig = {
      key: CREDENTIALS.youtube.API_KEY,
      part: "contentDetails",
      forUsername: user
    };
    
    Youtube.channels.list(channelConfig, function (error, data) {
    
      if (error) {
        console.log("Error fetching YouTube user video list", error);
        return;
      }
    
      // Get the uploads playlist Id
      let uploadsPlaylistId = data.items[0].contentDetails.relatedPlaylists.uploads;
    
      let playlistConfig = {
        part : 'snippet',
        maxResults : size,
        playlistId : uploadsPlaylistId,
        key: CREDENTIALS.youtube.API_KEY
      };
    
      // Fetch items from upload playlist
      Youtube.playlistItems.list(playlistConfig, function (error, data) {
    
        if (error) {
          console.log("Error fetching YouTube user video list", error);
        }
    
        doSomethingWithYourData(data.items);
      });
    });
    
  • 0

    在node.js中,可以使用以下代码实现 .

    需要 authKeychannelId 作为 options 对象参数 .

    获取数据后调用 cb 回调 .

    async function fetchChannelInfo(options) {
      const channelUrl = `https://www.googleapis.com/youtube/v3/channels?part=contentDetails,statistics&id=${
        options.channelId
      }&key=${options.authKey}`;
      const channelData = await axios.get(channelUrl);
    
      return channelData.data.items[0];
    }
    function fetch(options, cb) {
      fetchChannelInfo(options).then((channelData) => {
        options.playlistId = channelData.contentDetails.relatedPlaylists.uploads;
        const paylistUrl = `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=${
          options.playlistId
        }&key=${options.authKey}`;
    
        axios
          .get(paylistUrl)
          .then((response) => {
            const payloadData = ;
    
            const videoList = [];
            response.data.items.forEach((video) => {
              videoList.push({
                publishedAt: video.snippet.publishedAt,
                title: video.snippet.title,
                thumbnails: thumbnails,
                videoId: video.snippet.resourceId.videoId,
              });
            });
    
            cb(null, videoList);
          })
          .catch((err) => {
            cb(err, null);
          });
      });
    }
    

    注意:axios用于RESTful请求 . 安装

    npm install axios
    
  • 111
    $.get(
        "https://www.googleapis.com/youtube/v3/channels",{
          part: 'snippet,contentDetails,statistics,brandingSettings',
          id: viewid,
          key: api},
          function(data){
    
            $.each(data.items, function(i, item){
    
    
              channelId = item.id;
              pvideo = item.contentDetails.relatedPlaylists.uploads;
              uploads(pvideo);
    });
    
          });
    

    上传功能可以

    function uploads(pvideo){
    
    
           $.get(
            "https://www.googleapis.com/youtube/v3/playlistItems",{
              part: 'snippet',
              maxResults:12,
              playlistId:pvideo,
              key: api},
              function(data){
    
    
                $.each(data.items, function(i, item){
    
                     videoTitle = item.snippet.title;
                 videoId = item.id;
                description = item.snippet.description;
                thumb = item.snippet.thumbnails.high.url;
                channelTitle = item.snippet.channelTitle;
                videoDate = item.snippet.publishedAt;
                Catagoryid = item.snippet.categoryId;
                cID = item.snippet.channelId;
    
                })
              }
            );
         }
    
  • 0
    function tplawesome(e,t){res=e;for(var n=0;n<t.length;n++){res=res.replace(/\{\{(.*?)\}\}/g,function(e,r){return t[n][r]})}return res}
    
    
    
    $(function() {
    
    
        $(".form-control").click(function(e) {
    
    
           e.preventDefault();
    
    
           // prepare the request
    
    
           var request = gapi.client.youtube.search.list({
    
    
                part: "snippet",
    
    
                type: "video",
    
    
                q: encodeURIComponent($("#search").val()).replace(/%20/g, "+"),
    
    
                maxResults: 20,
    
    
                order: "viewCount",
    
    
                publishedAfter: "2017-01-01T00:00:00Z"
    
    
           }); 
    
    
           // execute the request
    
    
           request.execute(function(response) {
    
    
              var results = response.result;
    
    
              $("#results").html("");
    
    
              $.each(results.items, function(index, item) {
    
    
                $.get("tpl/item.html", function(data) {
    
    
                    $("#results").append(tplawesome(data, [{"title":item.snippet.title, "videoid":item.id.videoId ,"descrip":item.snippet.description ,"date":item.snippet.publishedAt ,"channel":item.snippet.channelTitle ,"kind":item.id.kind ,"lan":item.id.etag}]));
    
    
                });
    
    
                
    
    
              });
    
    
              resetVideoHeight();
    
    
           });
    
    
        });
    
    
        
    
    
        $(window).on("resize", resetVideoHeight);
    
    
    });
    
    
    
    function resetVideoHeight() {
    
    
        $(".video").css("height", $("#results").width() * 9/16);
    
    
    }
    
    
    
    function init() {
    
    
        gapi.client.setApiKey("YOUR API KEY .... USE YOUR KEY");
    
    
        gapi.client.load("youtube", "v3", function() {
    
    
            // yt api is ready
    
    
        });
    
    
    }
    

    在这里查看完整代码https://thecodingshow.blogspot.com/2018/12/youtube-search-api-website.html

相关问题