【问题标题】:Youtube API v3 Listing PlaylistItems Always Needs OAuthYoutube API v3 列出 PlaylistItems 总是需要 OAuth
【发布时间】:2020-08-18 06:12:14
【问题描述】:

我一直在研究这个问题,

我正在使用 youtube API 列出频道中的所有播放列表项目,我已阅读以下内容: Youtube Data API - How to avoid Google OAuth redirect URL authorization

但我仍然需要在每次运行时打开浏览器并验证我的程序才能访问我自己的 youtube 帐户。

根据此页面: https://developers.google.com/youtube/v3/getting-started

4. If your application will use any API methods that require user authorization, 
read the authentication guide to learn how to implement OAuth 2.0 authorization.

我只需要在频道中列出所有可用的视频。

google提供的代码示例:

import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
from google_auth_oauthlib.flow import Flow

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "CREDFILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.playlistItems().list(
        part="snippet",
        maxResults=30,
        playlistId="PLAYLISTID"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

我已将我的 credential.json 文件作为指定的代码,但我仍然需要对自己进行身份验证, 然后将身份验证代码粘贴到命令行中。我对在这里提供 credential.json 文件的使用感到困惑。

似乎是credentials = flow.run_console() 造成的, 是否有可能有其他方法来获取凭证?

【问题讨论】:

  • 请注意:要列出给定频道的所有 公开 视频,根本不需要使用 OAuth 授权流程!拥有一个有效的(可从 Google 控制台免费获取)API 应用程序密钥并将其传递给 PlaylistItems.list 端点就足够了。

标签: python youtube-api google-oauth youtube-data-api google-api-python-client


【解决方案1】:

您只需要用户身份验证即可获取详细信息或对您自己的播放列表进行修改。从您的问题和代码来看,您似乎只想从任何播放列表中获取视频列表。

您无需用户身份验证即可获取播放列表中的公共项目列表。您只需使用 API 密钥对 API playlistItems 端点进行 HTTP 调用。

来自页面https://developers.google.com/youtube/v3/docs

每个请求都必须指定 API 密钥(使用 key 参数)或提供 OAuth 2.0 令牌。您的 API 密钥在您的项目的开发者控制台的 API 访问窗格中可用。

您的 HTTP 调用将如下所示:

https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=PLeskMkEaHJYc_B6QFIaEwps1LXUpXx0_s&maxResults=50&key=[API_KEY]

【讨论】:

    【解决方案2】:

    Oauth2 的工作方式是需要用户同意才能授予对用户数据的访问权限。通过请求用户授予您的应用程序访问权限的浏览器窗口,该同意被授予。

    您总是必须至少同意一次,YouTube API 无法解决这个问题。

    您正在查看的另一个问题 (Youtube Data API - How to avoid Google OAuth redirect URL authorization ) 仅说明一旦刷新令牌被存储一次,您就不需要再次加载它。这可以通过 Google api Java 客户端库实现,因为该库中内置了用户凭据存储。

    您必须在我认为不支持的 python 客户端库中编写类似的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 2017-09-19
      相关资源
      最近更新 更多