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

El *_*ino 3 python-3.x google-oauth youtube-data-api

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

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

它看起来像这样:

请访问此 URL 以授权此应用程序:https : //accounts.google.com/o/oauth2/auth?response_type=code&client_id=7932902759886-cb8ai84grcqshe24nn459ka46uh45ssj.apps.googleusercontent.com&redirect3Auriet%3Auth%Aurn3A %3Aoob&范围= HTTPS%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly&状态= zNVvgEyO47nmacvdEEAhDsQipY194k&提示=同意&ACCESS_TYPE =离线&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()
Run Code Online (Sandbox Code Playgroud)

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

wll*_*bll 5

Credentials实例credentials = flow.run_console()具有刷新令牌的内置功能。如果需要,它将在执行请求时刷新令牌。

因此,您可以将credentials对象保存到pickle中,并在需要时读取它

对 Google python 示例代码进行一些修改:

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 = 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 build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
Run Code Online (Sandbox Code Playgroud)


Ana*_*ran 5

# -*- 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()
Run Code Online (Sandbox Code Playgroud)