I would like to add the Youtube api to my project to load video from my site, until now i just used languages like Html, css, javascript, php and sql and now i saw at this link https://learndataanalysis.org/how-to-upload-a-video-to-youtube-using-youtube-data-api-in-python/ a tutorial about how implement the youtube api using python (where i have 0 knowledge).
These are the codes i'm using:
First one named "upload_video.py"
import datatimefrom Google import Create_Servicefrom googleapiclient.http import MediaFileUploadCLIENT_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()
Second one named "Google.py"
import pickleimport osfrom google_auth_oauthlib.flow import Flow, InstalledAppFlowfrom googleapiclient.discovery import buildfrom googleapiclient.http import MediaFileUpload, MediaIoBaseDownloadfrom google.auth.transport.requests import Requestdef 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_fileAPI_SERVICE_NAME = api_nameAPI_VERSION = api_versionSCOPES = [scope for scope in scopes[0]]print(SCOPES)cred = Nonepickle_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 serviceexcept Exception as e: print('Unable to connect.') print(e) return Nonedef 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
Then the JSON file named "client_secret_194676583237-td2pjjvuhtad5dfg1fn65r9g0jin1mmm.apps.googleusercontent.com.json" that I created on Google Cloud Platform ( i have created the project, the OAuth 2.0 Client IDs and added the Api).
So my first question is:
- How i make this code working?
- What i'm missing?
In the tutorial video its working just with this code, but i can't make it run.
Second question should I maybe try to use php to run the API? or is better Python?
Note: I already verified the channel on youtube and i use Sublime text 3 and Xampp.