如何解析 Youtube Data API v3 : TypeError: response.result.items[



我使用了 Youtube Data API v3 - Google 开发者网站上列出的示例代码,其中我在本地主机上完成了授权过程来加载我上传的视频,但之后没有任何显示。我在控制台中检查了"响应"变量,它是空字符串......没有什么可以告诉我发生了什么以及为什么response.result.items[0]值未定义。我用这个确切的错误检查了其他 stackoverflow 答案,但没有一个与我的答案有关,例如必须playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads; playlistId = response.result.items[0].contentDetails.uploads;但仍然在代码示例中引用my_uploads.js遇到这个未定义的错误。我该如何解决此问题?

我添加到身份验证.js代码的唯一内容是:

gapi.client.setApiKey('xxxxxxxx');就在gapi.client.load之前

my_uploads.js:

// Define some variables used to remember state.
var playlistId, nextPageToken, prevPageToken;
// After the API loads, call a function to get the uploads playlist ID.
function handleAPILoaded() {
  requestUserUploadsPlaylistId();
}
// Call the Data API to retrieve the playlist ID that uniquely identifies the
// list of videos uploaded to the currently authenticated user's channel.
function requestUserUploadsPlaylistId() {
  // See https://developers.google.com/youtube/v3/docs/channels/list
  var request = gapi.client.youtube.channels.list({
    mine: true,
    part: 'contentDetails'
  });
  request.execute(function(response) {
    playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
    requestVideoPlaylist(playlistId);
  });
}
// Retrieve the list of videos in the specified playlist.
function requestVideoPlaylist(playlistId, pageToken) {
  $('#video-container').html('');
  var requestOptions = {
    playlistId: playlistId,
    part: 'snippet',
    maxResults: 10
  };
  if (pageToken) {
    requestOptions.pageToken = pageToken;
  }
  var request = gapi.client.youtube.playlistItems.list(requestOptions);
  request.execute(function(response) {
    // Only show pagination buttons if there is a pagination token for the
    // next or previous page of results.
    nextPageToken = response.result.nextPageToken;
    var nextVis = nextPageToken ? 'visible' : 'hidden';
    $('#next-button').css('visibility', nextVis);
    prevPageToken = response.result.prevPageToken
    var prevVis = prevPageToken ? 'visible' : 'hidden';
    $('#prev-button').css('visibility', prevVis);
    var playlistItems = response.result.items;
    if (playlistItems) {
      $.each(playlistItems, function(index, item) {
        displayResult(item.snippet);
      });
    } else {
      $('#video-container').html('Sorry you have no uploaded videos');
    }
  });
}
// Create a listing for a video.
function displayResult(videoSnippet) {
  var title = videoSnippet.title;
  var videoId = videoSnippet.resourceId.videoId;
  $('#video-container').append('<p>' + title + ' - ' + videoId + '</p>');
}
// Retrieve the next page of videos in the playlist.
function nextPage() {
  requestVideoPlaylist(playlistId, nextPageToken);
}
// Retrieve the previous page of videos in the playlist.
function previousPage() {
  requestVideoPlaylist(playlistId, prevPageToken);
}

它应该是response.data.items[0]而不是response.result.items[0]

response的类型为 AxiosResponse (https://github.com/axios/axios/blob/master/index.d.ts),它具有data字段。

相关内容

  • 没有找到相关文章

最新更新