【发布时间】: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