Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

A coomplete question how to upload video on youtube

$
0
0

I do not know why but Google is evil in changing APIs it is constantly evolving and We like developers always have to change our code. I want to upload a video with YOutube API but always I have 2 issues the first is that I can implement a resumable video upload but I can not implement it with refers token If the refresh token works resumable video does not work if the resumable video worked refers tokens does not. I am trying to solute it for one week and I still don't know so please can you give me a full answer.

my resumable upload:

import http.clientimport httplib2import osimport randomimport timeimport datetimefrom googleapiclient.discovery import buildfrom googleapiclient.errors import HttpErrorfrom googleapiclient.http import MediaFileUploadfrom google_auth_oauthlib.flow import InstalledAppFlowimport httplib2httplib2.RETRIES = 1MAX_RETRIES = 10RETRIABLE_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)RETRIABLE_STATUS_CODES = [500, 502, 503, 504]CLIENT_SECRETS_FILE = 'youtube_client.json'SCOPES = ['https://www.googleapis.com/auth/youtube.upload']API_SERVICE_NAME = 'youtube'API_VERSION = 'v3'VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')upload_date_time = datetime.datetime(2022, 12, 25, 12, 30, 0).isoformat() +'.000Z'request_body = {'snippet': {'categoryI': 10,'title': 'best music on the youtube | happy mood mix | AMP','description': "test",'tags': ['Travel', 'video test', 'Travel Tips']    },'status': {'privacyStatus': 'private','publishAt': upload_date_time,'selfDeclaredMadeForKids': False,     },'notifySubscribers': False}def get_authenticated_service():  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)  credentials = flow.run_console()  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)def initialize_upload(youtube,body,file):  insert_request = youtube.videos().insert(    part='snippet,status',    body=body,    media_body=MediaFileUpload(file, chunksize=-1, resumable=True))  response = resumable_upload(insert_request)  return responsedef resumable_upload(request):  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('Video id "%s" was successfully uploaded.' % 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)  return response['id']if __name__ == '__main__':  youtube = get_authenticated_service()  try:    response = initialize_upload(youtube,request_body,"output.mp4" )  except for HttpError as e:    print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content))  youtube.thumbnails().set(    videoId=response.get('id'),    media_body=MediaFileUpload('thumbnail.png')  ).execute()

I will not give you a code to refresh the token because it was absolutely bad.Thanks.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>