1

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)

1 Answer 1

1

The invalid_grant error is a catch all error for a lot of things. Currently invalid_grant normally means that your refresh token has expired.

A Google Cloud Platform project with an OAuth consent screen configured for an external user type and a publishing status of "Testing" is issued a refresh token expiring in 7 days.

The trick is to go to Google cloud console and set your project to production then your refresh tokens will last longer then seven days

enter image description here

As for your code you need to get it to request a new access token as the one you have is no longer working. So delete what ever is in store

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.