gpt4 book ai didi

python-3.x - Youtube Data API v.3 - 全自动 oAuth 流程(Python)?

转载 作者:行者123 更新时间:2023-12-04 00:02:29 25 4
gpt4 key购买 nike

我一直在探索 YouTube 数据 API。我的项目的前提很简单:使用 API,进行身份验证(是的,我有帐户的凭据),然后简单地检索我的所有视频列表,公共(public)和私有(private)。

我已经能够成功地完成这个,除了完全自动化的部分。我使用了各种来源的代码,当我在命令行上运行它时,它为我提供了一个在浏览器中使用的链接,以便进行授权。

它看起来像这样:

请访问此 URL 以授权此应用程序:https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=7932902759886-cb8ai84grcqshe24nn459ka46uh45ssj.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly&state=zNVvgEyO47nmacvdEEAhDsQipY194k&prompt=consent&access_type=offline&code_challenge=aF7uTCghjwgwjg49o3fgiIU-_ryK19rDeX4l1uzr37w&code_challenge_method=S256
输入授权码:

……

这是我的python代码片段:

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
...
...

# 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)
## MAKE youtube SEARCH REQUEST
last_date = '2018-10-01T00:00:00Z'
request = youtube.search().list(
part="snippet",
forMine=True,
maxResults=50,
order="date",
type="video"
)
all_items = []
response = request.execute()

我的问题如下: 是否可以以编程方式执行授权,以便应用程序可以独立运行而不必等待此用户操作 (要从 CMD 复制 URL,访问以获取 token ,然后再次复制并粘贴 token )?我想安排这个,因此希望它在没有人工干预的情况下运行和验证。这可能吗?如果是, 有人可以指点我一些工作示例和/或其他资源来帮助我到达那里吗?太感谢了。

最佳答案

# -*- coding: utf-8 -*-
# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
#!/usr/bin/python3.7
import os
import pickle
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
client_secrets_file = "client_secret.json"
api_service_name = "youtube"
api_version = "v3"

def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
youtube = get_authenticated_service()
request = youtube.channels().list(
part="contentDetails",
mine=True
)
response = request.execute()
print(response)

def get_authenticated_service():
if os.path.exists("CREDENTIALS_PICKLE_FILE"):
with open("CREDENTIALS_PICKLE_FILE", 'rb') as f:
credentials = pickle.load(f)
else:
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
credentials = flow.run_console()
with open("CREDENTIALS_PICKLE_FILE", 'wb') as f:
pickle.dump(credentials, f)
return googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
if __name__ == "__main__":
main()

关于python-3.x - Youtube Data API v.3 - 全自动 oAuth 流程(Python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58073119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com