【发布时间】:2021-12-03 13:18:14
【问题描述】:
我想将 Youtube api 添加到我的项目中以从我的网站加载视频,直到现在我只使用 Html、css、javascript、php 和 sql 等语言,现在我在这个链接上看到了 https://learndataanalysis.org/how-to-upload-a-video-to-youtube-using-youtube-data-api-in-python/ 一个关于如何使用 python 实现 youtube api(我有 0 知识)。
这些是我正在使用的代码:
- 第一个名为“upload_video.py”
import datatime
from Google import Create_Service
from googleapiclient.http import MediaFileUpload
CLIENT_SECRET_FILE = 'client_secret.json'
API_NAME = 'youtube'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/youtube.upload']
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
upload_date_time = datetime.datetime(2021, 11, 25, 12, 30, 0).isoformat() + '.000Z'
#print(“Site to refer: https://developers.google.com/youtube/v3/docs/videos/insert#.net”)
request_body = {
'snippet': {
'categoryId': 19,
'title': 'Uploading Test',
'description': '1° test di caricamento su youtube',
'tags': ['Travel', 'video test', 'Treavel tips']
},
'status': {
'privacyStatus': 'private',
'publishAt': upload_date_time,
'selfDeclareMadeForKids': False
},
'notifySubscribers': False
}
mediaFile = MediaFileUpload('../Video/Clouds - 64767.MP4')
response_upload = service.videos().insert(
part= 'snippet, status',
body= request_body,
media_body= mediaFile
).execute()
service.thumbnails().set(
videoId= response_upload.get('id'),
media_body= MediaFileUpload('../img/Thumbnail_Video/logo_STM.png')
).execute()
-第二个名为“Google.py”
import pickle
import os
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from google.auth.transport.requests import Request
def Create_Service(client_secret_file, api_name, api_version, *scopes):
print(client_secret_file, api_name, api_version, scopes, sep='-')
CLIENT_SECRET_FILE = client_secret_file
API_SERVICE_NAME = api_name
API_VERSION = api_version
SCOPES = [scope for scope in scopes[0]]
print(SCOPES)
cred = None
pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
# print(pickle_file)
if os.path.exists(pickle_file):
with open(pickle_file, 'rb') as token:
cred = pickle.load(token)
if not cred or not cred.valid:
if cred and cred.expired and cred.refresh_token:
cred.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
cred = flow.run_local_server()
with open(pickle_file, 'wb') as token:
pickle.dump(cred, token)
try:
service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
print(API_SERVICE_NAME, 'service created successfully')
return service
except Exception as e:
print('Unable to connect.')
print(e)
return None
def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
return dt
-然后是我在 Google Cloud Platform 上创建的名为“client_secret_194676583237-td2pjjvuhtad5dfg1fn65r9g0jin1mmm.apps.googleusercontent.com.json”的 JSON 文件(我创建了项目、OAuth 2.0 客户端 ID 并添加了 Api)。
所以我的第一个问题是:我如何让这段代码工作?我缺少什么?(在教程视频中,它只使用这段代码,但我无法让它运行)
第二个问题我是否应该尝试使用 php 来运行 API?还是更好的 Python?
注意:我已经在 youtube 上验证了频道,我使用的是 Sublime text 3 和 Xampp。
【问题讨论】:
-
如果你在让别人的代码工作时遇到困难——尤其是如果你不认为代码有问题,而是你必须做一些外部的事情来“让它运行”——那么您需要询问该代码的作者(或阅读相关文档等),而不是在这里询问。 Stack Overflow 不是技术支持,也不是论坛。请阅读How to Ask 了解主题内容。
-
欢迎加入堆栈,请记住在发布问题时说明您的问题。究竟是什么不适用于您的代码。遵循官方示例 Python quickstart 可能会有所帮助,但它是已安装的应用程序而不是 Web 应用程序。
标签: python youtube-api google-oauth youtube-data-api google-api-python-client