Search code examples
pythonpython-3.xyoutube-data-api

YouTube upload documentation code fails to get authenticated service due to OAuth2client AttributeError


I am uploading videos to YouTube programmatically following the official guide. This worked three weeks ago when I first tried and I logged into my account on a browswer for the OAuth2 flow.

Now my credentials expired and I cannot upload again. This is the relevant part of the code, copied from the sample code:

def get_authenticated_service():
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    scope=YOUTUBE_UPLOAD_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

  storage = Storage("%s-oauth2.json" % sys.argv[0])
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage, [])

  return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))

I now get this error from the oauth2client:

/usr/local/lib/python3.11/site-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access upload_youtube.py-oauth2.json: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Traceback (most recent call last):
  File "~/upload_youtube.py", line 291, in <module>
    youtube = get_authenticated_service()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/upload_youtube.py", line 92, in get_authenticated_service
    credentials = run_flow(flow, storage, [])
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/oauth2client/tools.py", line 195, in run_flow
    logging.getLogger().setLevel(getattr(logging, flags.logging_level))
                                                  ^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'logging_level'

I uninstalled and reinstalled google-api-python-client and oauth2client to make sure I had the latest version, and I still get the same error.

Note: The file upload_youtube.py-oauth2.json is a cache of the authenticated service so I don't have to login on the browser every time I run it. That authentication expires after 1-2 weeks. The warning (UserWarning: Cannot access upload_youtube.py-oauth2.json: No such file or directory) happens when the file is missing, e.g. when I run the code for the first time or (in this case), when I delete that file in an attempt to solve the error.

How can I work around this error and get an authenticated YouTube service?


Solution

  • I think the problem is in the third argument of the run_flow function, you specified it with an empty list, but that is wrong, you should use args = argparser.parse_args(), instead of an empty list, because it returns a list with some default objects, among which is “logging_level”, the absence of which is indicated by the interpreter