【问题标题】:How can I get the payload from calling YouTube API V3?如何通过调用 YouTube API V3 获取有效负载?
【发布时间】:2022-02-05 14:07:54
【问题描述】:

我正在尝试使用 Youtube API V3 从 YouTube 频道获取数据。下面的代码获取各种数据点,但是当我尝试运行应用程序时,我没有在响应对象中看到有效负载(“项目”)数组,也没有收到“错误”。这是我看到的:

 {
  kind: 'youtube#channelListResponse',
  etag: 'PqCO86vmlUDWFiOBSqOsOZRp5jE',
  pageInfo: { totalResults: 0, resultsPerPage: 50 }
}

我的代码看起来像这样 - 我做错了什么?

const Youtube = ({ youtubeDataJson }) => {
  console.log(youtubeDataJson);
  return (
    <>
      <div>
        <h1>This should log the data</h1>
      </div>
    </>
  );
};

export async function getServerSideProps(context) {
  //Base Url
  const gBaseUrl = "https://www.googleapis.com/youtube/v3";
  const channelName = "ThePrimeagen";

  //  fetching data
  const youtubeData = await fetch(
    `${gBaseUrl}/channels?part=snippet&part=contentDetails&part=statistics&part=contentOwnerDetails&forUsername=${channelName}&maxResults=50&key=${process.env.YOUTUBE_API_KEY}`
  );
  const youtubeDataJson = await youtubeData.json();

  //Return data
  return {
    props: {
      youtubeDataJson
    }
  };
}

export default Youtube;

【问题讨论】:

    标签: node.js next.js youtube-api


    【解决方案1】:

    请注意,totalResults 为 0。如果您有 0 个结果,您将不会收到 items 属性。当您输入未找到的 forUsername 时会发生这种情况。在您上面的代码中,ThePrimeagen 不是用户名。

    【讨论】:

    • 一开始我也是这么想的,但是频道确实存在:youtube.com/c/ThePrimeagen,我猜是用户名也是频道名称。我还尝试了其他一些频道名称,它们都返回了相同的问题。
    • 这不是用户名。当订阅者超过 100 人时,您可以选择自定义名称,但它不是用户名(或频道 ID)。 url 中的 c 代表自定义,而不是频道。
    • 尝试使用频道 ID 作为该自定义 URL 的用户名:UCUyeluBRhGPCW4rPe_UvBZQ
    • 感谢您的回复。非常感谢
    • 欢迎接受我的回答以帮助他人。如果也有用,请点赞。谢谢。
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2017-07-25
    • 2016-04-14
    • 2016-09-07
    相关资源
    最近更新 更多