使用Python的具有多个帐户的YouTube数据API

2024-05-19 00:00:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个应用程序,它可以帮助我的一个朋友更好地组织他的YouTube频道。他在不同的谷歌账户上拥有多个渠道。我是用Python开发的,目前我对YouTube数据API没有太多的经验,我计划使用它,因为它似乎是唯一的选择

应用程序本身并不复杂。它唯一需要做的事情就是上传视频,带有指定的标题、描述和其他属性,还可以在视频上写评论。我在Google开发者控制台中启动了一个简单的应用程序,启用了YouTube数据API,并创建了一个API密钥和一个OAUTH客户端ID

到目前为止,我已经设法在视频上发布评论,但每次我运行Python脚本(目前只是一个简单的脚本,只发布一条评论)时,Google都希望我明确选择要使用的帐户,并且每次运行脚本时,我都必须对脚本授予权限

有没有一种方法可以让我只运行一次脚本,然后告诉谷歌我想使用哪个帐户发布评论,授予所有权限,然后谷歌就会记住,这样我就不必每次都明确授予权限了

还有,我怎样才能切换帐户并使用该帐户进行上传,因为当前我总是需要在运行脚本时,当Google客户端弹出时选择一个帐户

我听说你可以获得谷歌授权的应用程序,这会有帮助吗?或者如果我只是让我的应用程序在测试中,而不是在生产中,这样可以吗


Tags: 数据脚本api应用程序权限客户端视频youtube
1条回答
网友
1楼 · 发布于 2024-05-19 00:00:59

如果您有N个帐户,并且希望在每个帐户上上载视频,则必须运行以成功完成NOAuth 2授权/身份验证流

对于这些NOAuth流中的每一个流,在成功完成每一个流后,必须将获得的凭证数据持久化到计算机本地存储中的一个单独的文件中

这可以被看作是应用程序的初始化步骤(不过,在以后的任何阶段,你都可以为你的应用程序需要知道的任何其他频道重复这一步骤)。您的代码如下所示:

# run an OAuth flow; then obtain credentials data
# relative to the channel the app's user had chosen
# during that OAuth flow
from google_auth_oauthlib.flow import InstalledAppFlow
scopes = ['https://www.googleapis.com/auth/youtube']
flow = InstalledAppFlow.from_client_secrets_file(
           client_secret_file, scopes)
cred = flow.run_console()

# build an YouTube service object such that to
# be able to retrieve the ID of the channel that
# the app's user had chosen during the OAuth flow
from googleapiclient.discovery import build
youtube = build('youtube', 'v3', credentials = cred)
response = youtube.channels().list(
    part = 'id',
    mine = True
).execute()
channel_id = response['items'][0]['id']

# save the credentials data to a JSON text file
cred_file = f"/path/to/credentials/data/dir/{channel_id}.json"
with open(cred_file, 'w', encoding = 'UTF-8') as json_file:
    json_file.write(cred.to_json())

上面,client_secret_file是包含应用程序客户端机密JSON文件的文件的完整路径,该文件是从Google开发者控制台获得的

随后,每次你想要上传视频时,你都必须在应用程序中选择将视频上传到哪个频道。从您的程序逻辑的角度来看,这意味着以下事情:假设您选择了ID为channel_id的频道:请读入与channel_id相关联的凭证数据文件,以便将其内容传递到YouTube服务对象youtube,构建如下:

# read in the credentials data associated to
# the channel identified by its ID 'channel_id'
from google.oauth2.credentials import Credentials
cred_file = f"/path/to/credentials/data/dir/{channel_id}.json"
cred = Credentials.from_authorized_user_file(cred_file)

# the access token need be refreshed when
# the previously saved one expired already
from google.auth.transport.requests import Request
assert cred and cred.valid and cred.refresh_token
if cred.expired:
    cred.refresh(Request())
    # save credentials data upon it got refreshed
    with open(cred_file, 'w', encoding = 'UTF-8') as json_file:
        json_file.write(cred.to_json())

# construct an YouTube service object through
# which any API invocations are authorized on
# behalf of the channel with ID 'channel_id'
from googleapiclient.discovery import build
youtube = build('youtube', 'v3', credentials = cred)

运行此代码后,YouTube服务对象youtube将以这样的方式初始化,即通过此对象发出的每个API端点调用将代表channel_id标识的通道完成授权请求

一个重要的注意事项:您需要安装包Google Authentication Library for Pythongoogle-auth,版本>;=1.21.3(google-authv1.3.0引入了Credentials.from_authorized_user_file,v1.8.0引入了Credentials.to_json,v1.21.3修复了后一个函数w.r.t.它的类expiry成员),以便将凭证对象cred保存到JSON文本文件中并从JSON文本文件中加载

还有一个重要的注意事项:上面的代码尽可能简化。根本不处理错误条件。例如,当在写入新凭据数据文件时cred_file已经存在,或者在读取假定已经存在的凭据数据时cred_file不存在时,上述代码不会处理错误情况

相关问题 更多 >

    热门问题