不同的 Youtube API 资源应该使用什么样的授权策略?

What kind of auth strategies should be used for distinct Youtube API's resources?

我开始使用 youtube API,我知道应用程序应该在 Google Cloud Platform 上启动,然后 Youtube API 必须 enabled .

因此,存在三种凭据:

到目前为止,我已经为浏览器使用创建了一个 API 键,从中我获得了评论和列表视频。但即使阅读文档,我也无法了解这些不同凭据 与 youtube API 需要哪种类型的凭据之间的 差异。

我还没有做任何与 youtube 用户相关的事情(上传视频、评论视频,喜欢视频),但纯数据消耗。

这些凭据之间有什么区别以及每个 Youtube 资源的需求?

我会像小龙虾一样,从最后一个到第一个:

服务帐号

既然你问的是 YouTube,我可以从一开始就告诉你,according to the docs服务帐户身份验证不能 用于 YouTube 资源:

Service accounts do not work for YouTube Data API calls because service accounts require an associated YouTube channel, and you cannot associate new or existing channels with service accounts. If you use a service account to call the YouTube Data API, the API server returns an error with the error type set to unauthorized and the reason set to youtubeSignupRequired.

这对于仅服务器身份验证很有用(当您不需要用户交互时)。

OAuth 2.0 应用程序 ID

这可能是最常见的身份验证方式:您的用户将被重定向以在他们的帐户中授予您的 Google 应用程序访问权限,然后您将获得一个用于获取访问令牌的代码。

显然,考虑到范围,这非常适合任何可用的 YouTube API 请求。

API键

您只能对不需要用户授权的请求(某些列表操作)使用 API 密钥。


所以,实际上,如果您想要 public 资源,只需使用 API 键即可。如果您需要访问用户数据,请使用 OAuth 2.0 工作流程。