【问题标题】:Embedding YouTube playlist with JSON - stopped working使用 JSON 嵌入 YouTube 播放列表 - 停止工作
【发布时间】:2015-08-19 03:41:12
【问题描述】:

我认为以下代码使用了已弃用的 YouTube API v2,以检索和显示播放列表。它工作了几个星期,现在已经停止 - 如果这是问题所在,是否有 API v3 方法可以做到这一点?

<script type="text/javascript">
var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk&alt=json&callback=?&max-results=50';
var videoURL= 'http://www.youtube.com/watch?v=';
$.getJSON(playListURL, function(data) {
    var list_data="";
    $.each(data.feed.entry, function(i, item) {
        var feedTitle = item.title.$t;
        var pubdate  = item.media$group.yt$uploaded.$t;
        var fulldate = new Date(pubdate).toLocaleDateString();
        var feedURL = item.link[1].href;
        var fragments = feedURL.split("/");
        var videoID = fragments[fragments.length - 2];
        var url = videoURL + videoID;
        var thumb = "http://img.youtube.com/vi/"+ videoID +"/mqdefault.jpg";
        list_data += '<li><a href="'+ url +'" title="'+ feedTitle +'"><img alt="'+ feedTitle+'" src="'+ thumb +'"</a><br /><a href="'+ url +'" title="'+ feedTitle +'"><strong>'+ feedTitle+'</strong></a><br />'+ fulldate +'</li>';
        console.log(item);
    });
    $(list_data).appendTo(".cont");
});
</script>
<ul class="cont" id="double">
</ul>

【问题讨论】:

    标签: json youtube youtube-api youtube-javascript-api


    【解决方案1】:

    YouTube v2 API 已被弃用,无法再使用。您将希望使用新的 PlaylistItems/list 端点从播放列表中获取视频信息。

    要从您指定的播放列表中获取视频,请尝试更改您的 GET 请求:

    请求:

    HTTP GET: https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2C+id&playlistId=PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk&key={YOUR_API_KEY}
    

    回复:

    {
     "kind": "youtube#playlistItemListResponse",
     "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/pBQT3k1NWD9D6HOvzNhT4QS1-W0\"",
     "nextPageToken": "CAUQAA",
     "pageInfo": {
      "totalResults": 10,
      "resultsPerPage": 5
     },
     "items": [
      {
    
       "kind": "youtube#playlistItem",
       "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/wu_RMwA6QYinWSMrYXyOugKDbyI\"",
       "id": "PLl3eZeHB033DOxfNMOVENodvC_4QtQ-r2bxEjeNprDxQ",
       "snippet": {
        "publishedAt": "2014-12-10T21:16:39.000Z",
        "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
        "title": "Private video",
        "description": "This video is private.",
        "channelTitle": "Late Show with David Letterman",
        "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
        "position": 0,
        "resourceId": {
         "kind": "youtube#video",
         "videoId": "4bVgilYncao"
        }
       }
      },
      {
    
       "kind": "youtube#playlistItem",
       "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/DPNyicQckvtlwgMTintYEwYsE6g\"",
       "id": "PLl3eZeHB033DOxfNMOVENoaZ-Ybk0W0zkTL1Sliq5xeA",
       "snippet": {
        "publishedAt": "2014-12-10T21:16:58.000Z",
        "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
        "title": "David Letterman Announces His Retirement from the Late Show",
        "description": "David Letterman announces that he will retire from the Late Show in 2015.\n\nLive on Letterman is up for a Webby award! Let your voice be heard and vote now! http://bit.ly/1l4oktE #WebbyAwards",
        "thumbnails": {
         "default": {
          "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/default.jpg",
          "width": 120,
          "height": 90
         },
         "medium": {
          "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/mqdefault.jpg",
          "width": 320,
          "height": 180
         },
         "high": {
          "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/hqdefault.jpg",
          "width": 480,
          "height": 360
         },
         "standard": {
          "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/sddefault.jpg",
          "width": 640,
          "height": 480
         },
         "maxres": {
          "url": "https://i.ytimg.com/vi/H9eYkpgeeI8/maxresdefault.jpg",
          "width": 1280,
          "height": 720
         }
        },
        "channelTitle": "Late Show with David Letterman",
        "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
        "position": 1,
        "resourceId": {
         "kind": "youtube#video",
         "videoId": "H9eYkpgeeI8"
        }
       }
      },
      {
    
       "kind": "youtube#playlistItem",
       "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/aL6XWPV0PoM50wQcyclAxsvtWy4\"",
       "id": "PLl3eZeHB033DOxfNMOVENobu4IxNcwxZw1eEEoLqHjhY",
       "snippet": {
        "publishedAt": "2014-12-10T21:17:14.000Z",
        "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
        "title": "Private video",
        "description": "This video is private.",
        "channelTitle": "Late Show with David Letterman",
        "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
        "position": 2,
        "resourceId": {
         "kind": "youtube#video",
         "videoId": "ONgN2Hgz3XE"
        }
       }
      },
      {
    
       "kind": "youtube#playlistItem",
       "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/YF1JYH8bTG20vjuPLeMR-2xkxjo\"",
       "id": "PLl3eZeHB033DOxfNMOVENoQncF7sXo86YALoy5U4yqlU",
       "snippet": {
        "publishedAt": "2014-12-10T21:17:30.000Z",
        "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
        "title": "David Letterman - Future Islands: \"Seasons (Waiting On You)\"",
        "description": "Making their network TV debut, Future Islands performed \"Seasons (Waiting On You)\" from their album, \"Singles.\"",
        "thumbnails": {
         "default": {
          "url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/default.jpg",
          "width": 120,
          "height": 90
         },
         "medium": {
          "url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/mqdefault.jpg",
          "width": 320,
          "height": 180
         },
         "high": {
          "url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/hqdefault.jpg",
          "width": 480,
          "height": 360
         }
        },
        "channelTitle": "Late Show with David Letterman",
        "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
        "position": 3,
        "resourceId": {
         "kind": "youtube#video",
         "videoId": "1Ee4bfu_t3c"
        }
       }
      },
      {
    
       "kind": "youtube#playlistItem",
       "etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/_Ft_7ujRwQbP7-altelmVnzwQx0\"",
       "id": "PLl3eZeHB033DOxfNMOVENoYQ9Z55P4r2KhvkV5yx-okc",
       "snippet": {
        "publishedAt": "2014-12-10T21:17:47.000Z",
        "channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
        "title": "Private video",
        "description": "This video is private.",
        "channelTitle": "Late Show with David Letterman",
        "playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
        "position": 4,
        "resourceId": {
         "kind": "youtube#video",
         "videoId": "Bzr5VtFvSyw"
        }
       }
      }
     ]
    }
    

    像往常一样解析 JSON。

    【讨论】:

      猜你喜欢
      • 2016-12-26
      • 2015-03-20
      • 1970-01-01
      • 2011-11-25
      • 2011-07-27
      • 1970-01-01
      • 2012-10-22
      • 2017-05-15
      • 1970-01-01
      相关资源
      最近更新 更多