I have a bot that uploads a Youtube video every 24 hours using the Youtube API. I have looked at the examples at https://github.com/SteBurz/youtube-uploader (which is just https://developers.google.com/youtube/v3/guides/uploading_a_video rewritten in Python 3), but after a week or so, it gives me invalid_grant when I try to upload a video.
Here is the part of the code that "builds" the API. I tried using the refresh method but that didn't seem to be the answer.
import http.client
import httplib2
import os
import random
import time
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
# References:
# https://developers.google.com/youtube/v3/guides/uploading_a_video
# https://github.com/SteBurz/youtube-uploader
CLIENT_SECRETS_FILE = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"..",
"credentials-youtube.json"
)
SCOPES = [
'https://www.googleapis.com/auth/youtube.upload',
'https://www.googleapis.com/auth/youtube.force-ssl',
]
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'
def get_authenticated_service():
credential_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '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)
else:
credentials.refresh(httplib2.Http())
return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
