I am coding to connect to the Youtube API, and the examples I've seen use "run" from the Oauthlib2 tools to run auth flows. It seems that something is wrong on my venv install ( I have reinstalled it like 4 times already ) but it can't find run ... may it be a version issue ? I can import other parts of the library but not .tools run.
The code :
import httplib2
import os
import logging
from oauth2client import run
from oauth2client.file import Storage
# from oauth2client.client import AccessTokenRefreshError
from googleapiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from googleapiclient.errors import HttpError
import json
CLIENT_SECRETS_FILE = "client_secrets.json"
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
MISSING_CLIENT_SECRETS_MESSAGE = "Missing client secrets file"
def authenticate():
httplib2.debuglevel = 4
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
scope=YOUTUBE_READ_WRITE_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json")
credentials = storage.get()
if credentials is None or credentials.invalid:
print('invalid credentials')
credentials = run_flow(flow, storage)
service = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
tags = "classical music", "yehudi mehunin"
body = dict(
snippet=dict(
title="some title",
description="a description",
tags=tags,
categoryId="4"
),
status=dict(
privacyStatus="Private"
)
)
thingy = service.videos().insert(part=",".join(body.keys()), body=None, media_body=MediaFileUpload(
"1977.mp4", mimetype="video/mp4", chunksize=1024 * 1024, resumable=False))
thingy.execute()
authenticate()
error :
Traceback (most recent call last):
File "/home/xavier/Code/autotube/youtube3.py", line 4, in <module>
from oauth2client import run
ImportError: cannot import name 'run'
I'm strongly suggesting looking for a more recent example of the current oauth2client implementation. The old run
method has been removed from the library in august, 2015 and was replaced by run_flow()
in the tools module. Import it like this:
from oauth2client import tools
Then access it using tools.run_flow()
.
Update: Regarding your follow up question which flags you should enter programmatically, if no command args are available, the trick is to use an empty args list like this:
flags = tools.argparser.parse_args(args=[])
creds = tools.run_flow(flow, storage, flags)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With