gpt4 book ai didi

python - 通过 Youtube 上传,通过 api 设置为私有(private)(锁定)

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

enter image description here我一直在使用 youtube API 远程上传。但是在搞乱代码一段时间后,所有上传的视频都会得到 “私有(private)(锁定)”由于条款和政策 .由于“ 无法上诉此违规行为 ”,我也无法上诉。只是为了澄清我之前能够上传并且最近才开始收到此错误。
代码:
youtube-上传者

#!/usr/bin/python

import argparse
import http.client
import httplib2
import os
import random
import time
import videoDetails

import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
from google_auth_oauthlib.flow import InstalledAppFlow

from oauth2client import client # Added
from oauth2client import tools # Added
from oauth2client.file import Storage # Added


# Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves.
httplib2.RETRIES = 1

# Maximum number of times to retry before giving up.
MAX_RETRIES = 10

# Always retry when these exceptions are raised.
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, http.client.NotConnected,
http.client.IncompleteRead, http.client.ImproperConnectionState,
http.client.CannotSendRequest, http.client.CannotSendHeader,
http.client.ResponseNotReady, http.client.BadStatusLine)

# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]

CLIENT_SECRETS_FILE = 'client_secrets.json'

SCOPES = ['https://www.googleapis.com/auth/youtube.upload']
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'

VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')



def get_authenticated_service(): # Modified
credential_path = os.path.join('./', 'credentials.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRETS_FILE, SCOPES)
credentials = tools.run_flow(flow, store)
return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)

def initialize_upload(youtube, options):
tags = None
if options.keywords:
tags = options.keywords.split(',')

body=dict(
snippet=dict(
title=options.getFileName("video").split(".", 1)[0],
description=options.description,
tags=tags,
categoryId=options.category
),
status=dict(
privacyStatus=options.privacyStatus
)
)

# Call the API's videos.insert method to create and upload the video.
videoPath = "/Users\caspe\OneDrive\Documents\Övrigt\Kodning\AwsCSGO\Video\%s" % (options.getFileName("video"))
insert_request = youtube.videos().insert(
part=','.join(body.keys()),
body=body,
media_body=MediaFileUpload(videoPath, chunksize=-1, resumable=True)
)

resumable_upload(insert_request, options)

# This method implements an exponential backoff strategy to resume a
# failed upload.
def resumable_upload(request, options):
response = None
error = None
retry = 0
while response is None:
try:
print('Uploading file...')
status, response = request.next_chunk()
if response is not None:
if 'id' in response:
print ('The video with the id %s was successfully uploaded!' % response['id'])

# upload thumbnail for Video
options.insertThumbnail(youtube, response['id'])
else:
exit('The upload failed with an unexpected response: %s' % response)
except HttpError as e:
if e.resp.status in RETRIABLE_STATUS_CODES:
error = 'A retriable HTTP error %d occurred:\n%s' % (e.resp.status,
e.content)
else:
raise
except RETRIABLE_EXCEPTIONS as e:
error = 'A retriable error occurred: %s' % e

if error is not None:
print (error)
retry += 1
if retry > MAX_RETRIES:
exit('No longer attempting to retry.')

max_sleep = 2 ** retry
sleep_seconds = random.random() * max_sleep
print ('Sleeping %f seconds and then retrying...') % sleep_seconds
time.sleep(sleep_seconds)

if __name__ == '__main__':
args = videoDetails.Video()
youtube = get_authenticated_service()

try:
initialize_upload(youtube, args)
except HttpError as e:
print ('An HTTP error %d occurred:\n%s') % (e.resp.status, e.content)
视频详情
import os
from googleapiclient.http import MediaFileUpload

class Video:
description = "test description"
category = "22"
keywords = "test"
privacyStatus = "public"

def getFileName(self, type):
for file in os.listdir("/Users\caspe\OneDrive\Documents\Övrigt\Kodning\AwsCSGO\Video"):
if type == "video" and file.split(".", 1)[1] != "jpg":
return file
break
elif type == "thumbnail" and file.split(".", 1)[1] != "mp4":
return file
break

def insertThumbnail(self, youtube, videoId):
thumnailPath = "/Users\caspe\OneDrive\Documents\Övrigt\Kodning\AwsCSGO\Video\%s" % (self.getFileName("thumbnail"))

request = youtube.thumbnails().set(
videoId=videoId,
media_body=MediaFileUpload(thumnailPath)
)
response = request.execute()
print(response)

最佳答案

如果您查看 Video.insert 的文档您将在页面顶部找到以下内容。这是最近开始实现的一项新政策。
enter image description here
在您的应用程序通过验证之前,您上传的所有视频都将设置为私有(private)。您需要先通过审核,然后才能上传公共(public)视频。
请注意,一旦您的应用程序通过验证,这将不会自动将所有现有的先前上传的视频设置为公开,您需要自己执行此操作。

关于python - 通过 Youtube 上传,通过 api 设置为私有(private)(锁定),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64695324/

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