googleapis / google-auth-library-python

Google Auth Python Library
https://googleapis.dev/python/google-auth/latest/
Apache License 2.0
772 stars 305 forks source link

AttributeError: 'Credentials' object has no attribute 'authorize' #264

Closed artly closed 6 years ago

artly commented 6 years ago

Hi, I've encountered this message while running the code below.

from apiclient.discovery import build
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/youtube']
SERVICE_ACCOUNT_FILE = 'ytmentions.json'
credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
ytservice = build(YOUTUBE_API_SERVICE_NAME, 
            YOUTUBE_API_VERSION, 
            credentials=credentials)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-140-5d89754385bf> in <module>()
      1 ytservice = build(YOUTUBE_API_SERVICE_NAME, 
      2             YOUTUBE_API_VERSION,
----> 3             credentials=credentials)
      4 

~/anaconda3/lib/python3.6/site-packages/googleapiclient/_helpers.py in positional_wrapper(*args, **kwargs)
    128                 elif positional_parameters_enforcement == POSITIONAL_WARNING:
    129                     logger.warning(message)
--> 130             return wrapped(*args, **kwargs)
    131         return positional_wrapper
    132 

~/anaconda3/lib/python3.6/site-packages/googleapiclient/discovery.py in build(serviceName, version, http, discoveryServiceUrl, developerKey, model, requestBuilder, credentials, cache_discovery, cache)
    223       return build_from_document(content, base=discovery_url, http=http,
    224           developerKey=developerKey, model=model, requestBuilder=requestBuilder,
--> 225           credentials=credentials)
    226     except HttpError as e:
    227       if e.resp.status == http_client.NOT_FOUND:

~/anaconda3/lib/python3.6/site-packages/googleapiclient/_helpers.py in positional_wrapper(*args, **kwargs)
    128                 elif positional_parameters_enforcement == POSITIONAL_WARNING:
    129                     logger.warning(message)
--> 130             return wrapped(*args, **kwargs)
    131         return positional_wrapper
    132 

~/anaconda3/lib/python3.6/site-packages/googleapiclient/discovery.py in build_from_document(service, base, future, http, developerKey, model, requestBuilder, credentials)
    362 
    363       # Create an authorized http instance
--> 364       http = _auth.authorized_http(credentials)
    365 
    366     # If the service doesn't require scopes then there is no need for

~/anaconda3/lib/python3.6/site-packages/googleapiclient/_auth.py in authorized_http(credentials)
    102                                                    http=build_http())
    103     else:
--> 104         return credentials.authorize(build_http())
    105 
    106 

AttributeError: 'Credentials' object has no attribute 'authorize'

Also including my pip freeze output.

alabaster==0.7.10
anaconda-client==1.6.14
anaconda-navigator==1.8.5
anaconda-project==0.8.2
appnope==0.1.0
appscript==1.0.1
asn1crypto==0.24.0
astroid==1.5.3
astropy==2.0.2
Babel==2.5.3
backcall==0.1.0
backports.shutil-get-terminal-size==1.0.0
beautifulsoup4==4.6.0
bitarray==0.8.1
bkcharts==0.2
bleach==2.1.3
bokeh==0.12.10
boto==2.48.0
Bottleneck==1.2.1
cachetools==2.0.1
certifi==2018.4.16
cffi==1.11.5
chardet==3.0.4
click==6.7
cloudpickle==0.4.0
clyent==1.2.2
colorama==0.3.9
conda==4.5.4
conda-build==3.0.27
conda-verify==2.0.0
contextlib2==0.5.5
cryptography==2.2.2
cycler==0.10.0
Cython==0.26.1
cytoolz==0.8.2
dask==0.15.3
datashape==0.5.4
decorator==4.3.0
distributed==1.19.1
docutils==0.14
entrypoints==0.2.3
et-xmlfile==1.0.1
fastcache==1.0.2
filelock==2.0.12
Flask==0.12.2
Flask-Cors==3.0.3
gevent==1.2.2
glob2==0.5
gmpy2==2.0.8
google-api-python-client==1.6.7
google-auth==1.4.1
google-auth-httplib2==0.0.2
greenlet==0.4.12
h5py==2.7.0
heapdict==1.0.0
html5lib==1.0.1
httplib2==0.11.1
idna==2.6
imageio==2.2.0
imagesize==1.0.0
instagram-scraper==1.5.20
ipykernel==4.8.2
ipython==6.4.0
ipython-genutils==0.2.0
ipywidgets==7.2.1
isort==4.2.15
itsdangerous==0.24
jdcal==1.3
jedi==0.12.0
Jinja2==2.10
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.2.3
jupyter-console==5.2.0
jupyter-core==4.4.0
jupyterlab==0.27.0
jupyterlab-launcher==0.4.0
lazy-object-proxy==1.3.1
llvmlite==0.20.0
locket==0.2.0
lxml==4.1.0
MarkupSafe==1.0
matplotlib==2.1.0
mccabe==0.6.1
mistune==0.8.3
mkl-fft==1.0.0
mkl-random==1.0.1
mpmath==0.19
msgpack-python==0.4.8
multipledispatch==0.4.9
navigator-updater==0.1.0
nbconvert==5.3.1
nbformat==4.4.0
networkx==2.0
nltk==3.2.4
nose==1.3.7
notebook==5.4.1
numba==0.35.0+6.gaa35fb1
numexpr==2.6.5
numpy==1.13.3
numpydoc==0.8.0
oauth2client==4.1.2
odo==0.5.1
olefile==0.45.1
openpyxl==2.4.8
packaging==17.1
pandas==0.22.0
pandocfilters==1.4.2
parso==0.2.0
partd==0.3.8
path.py==11.0.1
pathlib2==2.3.2
patsy==0.4.1
pep8==1.7.0
pexpect==4.5.0
pickleshare==0.7.4
Pillow==5.1.0
pkginfo==1.4.1
ply==3.10
prompt-toolkit==1.0.15
psutil==5.4.5
ptyprocess==0.5.2
py==1.4.34
pyasn1==0.4.2
pyasn1-modules==0.2.1
pycodestyle==2.3.1
pycosat==0.6.3
pycparser==2.18
pycrypto==2.6.1
pycurl==7.43.0
pyflakes==1.6.0
Pygments==2.2.0
pylint==1.7.4
pyodbc==4.0.17
pyOpenSSL==17.5.0
pyparsing==2.2.0
PySocks==1.6.8
pytest==3.2.1
python-dateutil==2.7.2
pytz==2018.4
PyWavelets==0.5.2
PyYAML==3.12
pyzmq==17.0.0
QtAwesome==0.4.4
qtconsole==4.3.1
QtPy==1.4.1
requests==2.18.4
requests-file==1.4.3
rope==0.10.5
rsa==3.4.2
ruamel-yaml==0.15.35
scikit-image==0.13.0
scikit-learn==0.19.1
scipy==1.1.0
seaborn==0.8
selenium==3.11.0
Send2Trash==1.5.0
simplegeneric==0.8.1
simplejson==3.14.0
singledispatch==3.4.0.3
six==1.11.0
snowballstemmer==1.2.1
sortedcollections==0.5.3
sortedcontainers==1.5.10
Sphinx==1.7.4
sphinxcontrib-websupport==1.0.1
spyder==3.2.4
SQLAlchemy==1.2.7
statsmodels==0.8.0
sympy==1.1.1
tables==3.4.2
tblib==1.3.2
terminado==0.8.1
testpath==0.3.1
tldextract==2.2.0
toolz==0.8.2
tornado==5.0.2
tqdm==4.19.5
traitlets==4.3.2
typing==3.6.4
unicodecsv==0.14.1
uritemplate==3.0.0
urllib3==1.22
wcwidth==0.1.7
webencodings==0.5.1
Werkzeug==0.14.1
widgetsnbextension==3.2.1
wrapt==1.10.11
xgboost==0.71
xlrd==1.1.0
xlwt==1.2.0
zict==0.1.3

Any possible help would be highly appreciated.

theacodes commented 6 years ago

So I haven't been able to reproduce this with a fresh environment. Judging by the large amount of packages reported by pip freeze it appears you're not using a virtualenv. Can you see if you can reproduce with a few virtualenv?

My pip freeze:

cachetools==2.1.0
google-api-python-client==1.6.7
google-auth==1.4.1
google-auth-httplib2==0.0.2
httplib2==0.11.3
oauth2client==4.1.2
pyasn1==0.4.3
pyasn1-modules==0.2.1
rsa==3.4.2
six==1.11.0
uritemplate==3.0.0

My result:

>>> import os
>>> from apiclient.discovery import build
>>> from google.oauth2 import service_account
>>>
>>> SCOPES = ['https://www.googleapis.com/auth/youtube']
>>> SERVICE_ACCOUNT_FILE = os.environ['SERVICE_ACCOUNT']
>>> credentials = service_account.Credentials.from_service_account_file(
...         SERVICE_ACCOUNT_FILE, scopes=SCOPES)
>>>
>>> ytservice = build('youtube',
...             'v3',
...             credentials=credentials)
>>>
artly commented 6 years ago

@theacodes Many thanks, it certainly worked with a newly generated environment. Yay!

I've tried to run the script with your list of packages downgraded to the respective versions from my list but it still works.

So I would blame the presence of some of the other libraries in my environment. I've used anaconda default environment with a couple of additions needed for the project so it's hard to find out which package is responsible for the error as there are lots of them (you're right). Just to mention - the output above was not a pip freeze but conda list instead which is an analogical as I believe.

Thank you!