什么是资源?
了解资源及其各种类型。
资源 是一个具有唯一标识符的独立数据实体。它代表了 YouTube 体验中的某个项目,例如视频、频道、播放列表或字幕。YouTube Data API 支持多种不同的资源,我们可以利用它们来获取数据。在本课程中,我们仅讨论其中几种资源。
资源类型
下表列出了我们可以使用 YouTube Data API 与之交互的一些资源。
| 资源 | 描述 |
|---|---|
| activity | 包含用户或频道在 YouTube 网站上执行的操作信息。活动流中报告的操作包括分享视频、为视频评分、将视频标记为收藏等。 |
| channel | 包含关于 YouTube 频道的信息,如订阅者数量、观看次数、视频数量、频道名称和描述。 |
| search | 包含与 API 请求中指定的搜索参数匹配的 YouTube 视频或频道信息。可用于获取某个频道上传的所有视频列表。 |
| video | 代表一个 YouTube 视频。包含与搜索参数匹配的视频信息,例如视频标题、描述、观看次数等。 |
| commentThread | 包含视频或频道评论线程的信息。该线程由一条一级评论及其回复组成(如果存在)。 |
| i18nRegion | 标识 YouTube 支持的一个地理区域,用户可将其选为首选内容区域。它包含区域代码和名称。 |
| playlist | 代表一个 YouTube 播放列表。播放列表是一组可以依次观看的视频集合。 |
| playlistItem | 标识播放列表中包含的另一个资源,例如视频。 |
注意: 一个资源可能包含对其他资源的引用。例如,搜索结果可能包含
videoId或channelId属性,分别用于标识特定的视频或频道资源。
支持的方法
API 支持多种用于执行特定资源操作的方法。下表展示了 API 支持的最常用方法。
| 方法 | 描述 |
|---|---|
| list | 获取零个或多个资源的列表 |
| insert | 创建一个新的资源 |
| delete | 删除一个资源 |
| update | 修改现有资源 |
对资源使用 insert、update 或 delete 方法始终需要用户授权。list 方法支持已授权和未授权的请求。已授权请求可以获取私有用户的信息,而未授权请求只能获取公开数据。
下图展示了所有资源类型及其支持的方法、权限类型以及本课程将讨论的返回数据。
请求参数
使用 API 时,避免传输、解析和存储不必要的数据是良好的实践。为此,API 允许通过两个请求参数——part 和 fields——来获取部分资源。这些参数使我们能够指定 API 响应中应包含的资源属性。
part 参数
它标识应为特定资源返回的一个或多个属性。任何检索或返回资源的 API 请求都必须包含 part 参数。例如,video 资源包含以下部分:snippet、contentDetails、fileDetails、statistics、status 等。这些是最常使用的部分,将在后续课程中详细讨论。
所有这些部分都是带有嵌套属性的对象。我们可以将这些对象视为一组元数据字段,API 服务器可能会返回,也可能不返回。当我们讨论每个资源时,会更详细地分析这些部分。
fields 参数
它用于过滤 API 响应,仅包含通过 part 参数指定的资源部分,并且响应中只包含特定的一组字段。fields 参数还允许你从 API 响应中移除嵌套属性。
注意: 我们不能使用
part参数来过滤 API 响应中的嵌套属性。
以下是设置 fields 参数值的规则和语法:
- 使用逗号分隔的列表来选择多个字段。例如,
fields=a,b。 - 使用星号作为通配符以标识所有字段。例如,
fields=*。 - 使用括号来指定一组要包含在 API 响应中的嵌套属性。例如,
fields=a(b,c)。
下面通过一些示例来演示 part 和 fields 的用法。
注意: 目前我们尚未使用之前保存的
API_KEY。您可能会在查询 URL 中看到YOUR_API_KEY。可以暂时忽略这一点,因为我们将在接下来的课程中使用它来获取数据。
示例 1
假设我们希望 API 返回一个包含 snippet 和 statistics 部分的视频资源,同时包含 kind 和 etag 属性。
API URL:
1https://www.googleapis.com/youtube/v3/videos?id=Q7J5qrcPyns&key=YOUR_API_KEY&part=snippet,statistics
API 响应:
在以下 API 响应中,我们可以看到 snippet 和 statistics 部分的对象。每个对象都显示了关于该视频资源的信息。
1{ 2 "items": [ 3 { 4 "kind": "youtube#video", 5 "etag": "yxVgBpa9AVwYvYemndL8h_qiUxo", 6 "id": "Q7J5qrcPyns", 7 8 "snippet": { 9 "publishedAt": "2021-09-20T12:00:09Z", 10 "channelId": "UCT_8FqzTIr2Q1BOtvX_DPPw", 11 "title": "#77: \"Learning Code by Giving Back\" with Chibuike Nwachukwu of Spleet Africa | Educative Sessions", 12 "description": "When Chibuike Nwachukwu began his programming journey in college, it was really difficult for him to find his path. From his ever-changing interests to lack of mentorship and guidance, its a surprise he stayed focused. Now that he's matured, he sees to it that no junior coder he encounters has to go through the many avoidable hassles he went through.\n\nIn our session, Chibuike tells us how helping others helped him become a better coder and also help him find himself along the way. \n\nListen to the podcast HERE: https://www.podbean.com/media/share/pb-226yn-10dc3d7\n(Also available on Apple, Google, Spotify, and more major platforms!)\n\nABOUT OUR GUEST\n\nChibuike Nwachukwu is a self-educated software engineer and technical Writer. He is a passionate problem solver, who enjoys constant learning and knowledge sharing. When not coding, you would find him playing games or forming new partnerships.\nVisit Educative to start your journey into code ►► https://educative.io\n\nDon't forget to subscribe to Educative Sessions on YouTube! \n►► https://www.youtube.com/c/EducativeSessions/?sub_confirmation=1\n\nABOUT EDUCATIVE\n\nEducative (https://educative.io) provides interactive and adaptive courses for software developers. Whether it’s beginning to learn to code, grokking the next interview, or brushing up on frontend coding, data science, or cybersecurity, Educative is changing how developers continue their education. Stay relevant through our pre-configured learning environments that adapt to match a developer’s skill level. Educative provides the best author platform for instructors to create interactive and adaptive content in only a few clicks.\n\nMore Videos from Educative Sessions: https://www.youtube.com/c/EducativeSessions/\n\nSign Up to Speak on Educative Sessions: ►►\nhttp://bit.ly/educative-sessions-s3-signup\n\nEpisode 77: \"Learning Code by Giving Back\" with Chibuike Nwachukwu of Spleet Africa | Educative Sessions", 13 "thumbnails": { 14 "default": { 15 "url": "https://i.ytimg.com/vi/Q7J5qrcPyns/default.jpg", 16 "width": 120, 17 "height": 90 18 }, 19 "medium": { 20 "url": "https://i.ytimg.com/vi/Q7J5qrcPyns/mqdefault.jpg", 21 "width": 320, 22 "height": 180 23 }, 24 "high": { 25 "url": "https://i.ytimg.com/vi/Q7J5qrcPyns/hqdefault.jpg", 26 "width": 480, 27 "height": 360 28 }, 29 "standard": { 30 "url": "https://i.ytimg.com/vi/Q7J5qrcPyns/sddefault.jpg", 31 "width": 640, 32 "height": 480 33 } 34 } 35 }, 36 37 "statistics": { 38 "viewCount": "121", 39 "likeCount": "24", 40 "dislikeCount": "0", 41 "favoriteCount": "0", 42 "commentCount": "16" 43 } 44 } 45 ] 46}
示例 2
假设我们希望 API 返回一个包含 snippet 和 statistics 部分的视频资源,但排除 kind、etag 以及 snippet 对象中的部分嵌套属性。
我们将使用 fields 参数,使得 API 响应中仅返回 snippet 对象中的 channelId、title 和 categoryId 属性。
API URL:
1https://www.googleapis.com/youtube/v3/videos?id=Q7J5qrcPyns&key=YOUR_API_KEY&part=snippet,statistics&fields=items(id,snippet(channelId,title,categoryId),statistics)
API 响应:
以下 API 响应显示了 snippet 和 statistics 部分的对象已高亮。我们不再看到 kind、etag 以及之前示例中 snippet 对象的大部分嵌套属性。
1{ 2 "items": [ 3 { 4 "id": "Q7J5qrcPyns", 5 6 "snippet": { 7 "channelId": "UCT_8FqzTIr2Q1BOtvX_DPPw", 8 "title": "#77: \"Learning Code by Giving Back\" with Chibuike Nwachukwu of Spleet Africa | Educative Sessions", 9 "categoryId": "28" 10 }, 11 12 "statistics": { 13 "viewCount": "121", 14 "likeCount": "24", 15 "dislikeCount": "0", 16 "favoriteCount": "0", 17 "commentCount": "16" 18 } 19 } 20 ] 21}