iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
564 stars 70 forks source link

AuthenticationError: No code found in redirect #58

Closed LucSpan closed 3 years ago

LucSpan commented 3 years ago

Since yesterday I can't authenticate anymore.

Code

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

Error

Failed to find "code" in the query parameters of the redirect.
Try command-line authentication
Traceback (most recent call last):

  File "<ipython-input-36-792f41ab7318>", line 1, in <module>
    gauth.LocalWebserverAuth()

  File "/opt/anaconda3/lib/python3.7/site-packages/pydrive2/auth.py", line 125, in _decorated
    code = decoratee(self, *args, **kwargs)

  File "/opt/anaconda3/lib/python3.7/site-packages/pydrive2/auth.py", line 273, in LocalWebserverAuth
    raise AuthenticationError("No code found in redirect")

AuthenticationError: No code found in redirect    

client_secrets.json is in the working directory.

How do I solve this?

shcheklein commented 3 years ago

@LucSpan hi! I haven't been able to reproduce this on the latest version. I think we'll need more information to help you.

Could you check what do you have in the browser URL bar after you click Continue? What Authorized redirect URIs do you have in the application credentials belong to? (it should be http://localhost:8080/) Can you share pip freeze output please?

Let's also try a clean environment ?

git clone git@github.com:iterative/PyDrive2.git
cd PyDrive2
virtualenv -p python3 .env
source .env/bin/activate
pip install -e ".[tests]"

<run your test script>
LucSpan commented 3 years ago

Hi @shcheklein , thank you for your quick reply!

I have managed to connect via Google's quickstart.py: https://stackoverflow.com/questions/63956706/google-drive-api-quickstart-py-error-400-redirect-uri-mismatch.

Pip freeze output,

WARNING: Could not generate requirement for distribution -YAML 5.3.1 (/opt/anaconda3/lib/python3.7/site-packages): Parse error at "'-YAML==5'": Expected W:(abcd...)
WARNING: Could not generate requirement for distribution -yYAML 5.3.1 (/opt/anaconda3/lib/python3.7/site-packages): Parse error at "'-yYAML=='": Expected W:(abcd...)
WARNING: Could not generate requirement for distribution - YAML (/opt/anaconda3/lib/python3.7/site-packages): Parse error at "'-===YAML'": Expected W:(abcd...)
alabaster==0.7.12
anaconda-client==1.7.2
anaconda-navigator==1.9.7
anaconda-project==0.8.3
appdirs==1.4.3
appnope==0.1.0
appscript==1.0.1
asn1crypto==1.0.1
astroid==2.3.1
astropy==3.2.2
atomicwrites==1.3.0
attrs==19.2.0
Automat==0.8.0
Babel==2.7.0
backcall==0.1.0
backports.functools-lru-cache==1.5
backports.os==0.1.1
backports.shutil-get-terminal-size==1.0.0
backports.tempfile==1.0
backports.weakref==1.0.post1
bcrypt==3.1.7
beautifulsoup4==4.8.0
bitarray==1.0.1
bkcharts==0.2
bleach==3.1.0
blessings==1.7
bokeh==1.3.4
boto==2.49.0
Bottleneck==1.2.1
cached-property==1.5.1
cachetools==4.0.0
Cerberus==1.3.2
certifi==2019.11.28
cffi==1.12.3
chardet==3.0.4
chromedriver-py==83.0.4103.14
Click==7.0
cloudpickle==1.2.2
clyent==1.2.2
colorama==0.4.1
conda==4.7.12
conda-build==3.18.9
conda-package-handling==1.6.0
conda-verify==3.4.2
config42==0.4.2
constantly==15.1.0
contextlib2==0.6.0
cryptography==3.1
cssselect==1.1.0
currency.converter==0.5.5
CurrencyConverter==0.13.10
cycler==0.10.0
Cython==0.29.13
cytoolz==0.10.0
dask==2.5.2
decorator==4.4.0
defusedxml==0.6.0
distributed==2.5.2
docker==4.2.0
docker-compose==1.25.5
dockerpty==0.4.1
docopt==0.6.2
docutils==0.15.2
entrypoints==0.3
et-xmlfile==1.0.1
fake-useragent==0.1.11
fastcache==1.1.0
filelock==3.0.12
Flask==1.1.1
forex-python==1.5
fsspec==0.5.2
future==0.18.2
gevent==1.4.0
glob2==0.7
gmpy2==2.0.8
google-api-core==1.22.2
google-api-python-client==1.12.1
google-auth==1.21.2
google-auth-httplib2==0.0.4
google-auth-oauthlib==0.4.1
googleapis-common-protos==1.51.0
greenlet==0.4.15
gspread==3.1.0
h5py==2.9.0
HeapDict==1.0.1
html5lib==1.0.1
httplib2==0.14.0
huepy==1.2.1
hyperlink==19.0.0
idna==2.8
imageio==2.6.0
imageio-ffmpeg==0.4.1
imagesize==1.1.0
importlib-metadata==0.23
imutils==0.5.3
incremental==17.5.0
instabot==0.117.0
instabot-py==0.7.2
instaloader==4.2.8
ipykernel==5.1.2
ipython==7.8.0
ipython-genutils==0.2.0
ipywidgets==7.5.1
isodate==0.6.0
isort==4.3.21
itsdangerous==1.1.0
jdcal==1.4.1
jedi==0.15.1
Jinja2==2.10.3
joblib==0.13.2
json5==0.8.5
jsonschema==3.0.2
jupyter==1.0.0
jupyter-client==5.3.3
jupyter-console==6.0.0
jupyter-core==4.5.0
jupyterlab==1.1.4
jupyterlab-server==1.0.6
keyring==18.0.0
kiwisolver==1.1.0
lazy-object-proxy==1.4.2
libarchive-c==2.8
lief==0.9.0
llvmlite==0.29.0
locket==0.2.0
lxml==4.4.1
MarkupSafe==1.1.1
matplotlib==3.1.1
mccabe==0.6.1
mistune==0.8.4
mkl-fft==1.0.14
mkl-random==1.1.0
mkl-service==2.3.0
mock==3.0.5
more-itertools==7.2.0
moviepy==1.0.2
mpmath==1.1.0
msgpack==0.6.1
multipledispatch==0.6.0
navigator-updater==0.2.1
nbconvert==5.6.0
nbformat==4.4.0
networkx==2.3
nltk==3.4.5
nose==1.3.7
notebook==6.0.1
numba==0.45.1
numexpr==2.7.0
numpy==1.18.2
numpydoc==0.9.1
oauth2-client==1.1.0
oauth2client==4.1.3
oauthlib==3.1.0
olefile==0.46
openpyxl==3.0.0
ordereddict==1.1
packaging==19.2
pandas==0.25.1
pandocfilters==1.4.2
paramiko==2.7.1
parsel==1.5.2
parso==0.5.1
partd==1.0.0
path.py==12.0.1
pathlib2==2.3.5
patsy==0.5.1
paypalrestsdk==1.13.1
pep8==1.7.1
pexpect==4.7.0
pickleshare==0.7.5
Pillow==7.0.0
pkginfo==1.5.0.1
pluggy==0.13.0
ply==3.11
proglog==0.1.9
prometheus-client==0.7.1
prompt-toolkit==2.0.10
Protego==0.1.15
protobuf==3.13.0
psutil==5.6.3
ptyprocess==0.6.0
py==1.8.0
pyactiveresource==2.2.0
pyasn1==0.4.7
pyasn1-modules==0.2.7
pycodestyle==2.5.0
pycosat==0.6.3
pycparser==2.19
pycrypto==2.6.1
pycurl==7.43.0.3
PyDispatcher==2.0.5
PyDrive==1.3.1
PyDrive2==1.6.1
pyflakes==2.1.1
Pygments==2.4.2
PyHamcrest==1.9.0
pylint==2.4.2
PyNaCl==1.3.0
pyodbc==4.0.27
pyOpenSSL==19.1.0
pyparsing==2.4.2
PyQt5==5.12.3
PyQt5-sip==12.7.1
PyQtWebEngine==5.12.1
pyrsistent==0.15.4
PySocks==1.7.1
pytest==5.2.1
pytest-arraydiff==0.3
pytest-astropy==0.5.0
pytest-doctestplus==0.4.0
pytest-openfiles==0.4.0
pytest-remotedata==0.3.2
python-dateutil==2.8.0
pytz==2019.3
PyWavelets==1.0.3
PyYAML==5.1
pyzmq==18.1.0
QtAwesome==0.6.0
qtconsole==4.5.5
QtPy==1.9.0
queuelib==1.5.0
requests==2.22.0
requests-oauthlib==1.3.0
requests-toolbelt==0.9.1
responses==0.10.12
rope==0.14.0
rsa==4.0
ruamel-yaml==0.15.46
schedule==0.6.0
scikit-image==0.15.0
scikit-learn==0.21.3
scipy==1.3.1
Scrapy==1.8.0
scrapy-fake-useragent==1.2.0
scrapy-random-useragent==0.2
seaborn==0.9.0
selenium==3.141.0
Send2Trash==1.5.0
service-identity==18.1.0
ShopifyAPI==7.0.0
simplegeneric==0.8.1
simplejson==3.17.0
singledispatch==3.4.0.3
six==1.14.0
snowballstemmer==2.0.0
sortedcollections==1.1.2
sortedcontainers==2.1.0
soupsieve==1.9.3
Sphinx==2.2.0
sphinxcontrib-applehelp==1.0.1
sphinxcontrib-devhelp==1.0.1
sphinxcontrib-htmlhelp==1.0.2
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.2
sphinxcontrib-serializinghtml==1.1.3
sphinxcontrib-websupport==1.1.2
spyder==3.3.6
spyder-kernels==0.5.2
SQLAlchemy==1.3.11
statsmodels==0.10.1
suds-py3==1.3.4.0
sympy==1.4
tables==3.5.2
tblib==1.4.0
terminado==0.8.2
testpath==0.4.2
texttable==1.6.2
tika==1.23.1
toolz==0.10.0
tornado==6.0.3
tqdm==4.43.0
traitlets==4.3.3
Twisted==19.10.0
typed-ast==1.4.1
unicodecsv==0.14.1
uritemplate==3.0.1
urllib3==1.25.8
w3lib==1.21.0
wcwidth==0.1.7
webencodings==0.5.1
websocket-client==0.57.0
Werkzeug==0.16.0
widgetsnbextension==3.5.1
WooCommerce==2.1.1
wrapt==1.11.2
wurlitzer==1.0.3
xlrd==1.2.0
XlsxWriter==1.2.1
xlwings==0.15.10
xlwt==1.3.0
xmltodict==0.12.0
zeep==3.4.0
zict==1.0.0
zipp==0.6.0
zope.interface==4.7.1
shcheklein commented 3 years ago

@LucSpan sounds promising. Have you tried to run it in a clean virtual environment? Also, still curious what do you see in your browser, would be great if you could enable dev console and check + copy/paste network requests.

LucSpan commented 3 years ago

@shcheklein , when I run your clean virtual environment commands I get a,

Warning: Permanently added the RSA host key for IP address '140.82.118.3' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I copied the HTTP requests of all network requests.

null /signin/oauth/consent
null /s/roboto/v18/KFOmCnqEu92Fr1Mu7WxKKTU1Kvnz.woff2
null /s/roboto/v18/KFOmCnqEu92Fr1Mu4WxKKTU1Kvnz.woff2
null /signin/oauth/consent
null /s/roboto/v18/KFOlCnqEu92Fr1MmSU5fChc4AMP6lbBP.woff2
null /s/roboto/v18/KFOmCnqEu92Fr1Mu7GxKKTU1Kvnz.woff2
null /s/roboto/v18/KFOkCnqEu92Fr1MmgVxIIzIXKMny.woff2
null /s/roboto/v18/KFOlCnqEu92Fr1MmEU9fChc4AMP6lbBP.woff2
null /s/roboto/v18/KFOlCnqEu92Fr1MmEU9fBBc4AMP6lQ.woff2
null /s/roboto/v18/KFOlCnqEu92Fr1MmWUlfChc4AMP6lbBP.woff2
null /accounts/static/_/js/k=gaia.gaiafe_glif.en.ULVZ6XuenpA.O/am=740fMHAHRAIHHAACzAAAAAAAAACAAOABxL55UL1W8ZaLAQ/d=0/ct=zgms/rs=ABkqax0TQFzjaCyaVm2rUjd43zEzc3uuAg/m=lCVo3d,MB66Qc,zf3eV,zwU6q,OUAKhb,ZDlobb,O5seLe,uubwEd,qDruM,sy6d,Gt2gAf,L1AAkb,eV9nn,O6y8ed,aW3pY,Z7PiFb,CX9aud,nqpTHe,RZunBd,NAySvc,I6YDgd,BHEQ4d,sy6f,RF0g6
null /s/roboto/v18/KFOlCnqEu92Fr1MmWUlfBBc4AMP6lQ.woff2
null /s/roboto/v18/KFOkCnqEu92Fr1MmgVxGIzIXKMnyrYk.woff2
null /-biTxlbx0Okk/AAAAAAAAAAI/AAAAAAAAAAA/AMZuuckiXMpjGY54XS3-NmgFZ4GJ0KNPQg/s128-c/photo.jpg
null /s/googlesans/v14/4UabrENHsxJlGDuGo1OIlLU94YtzCwZsPF4o.woff2
null /s/roboto/v18/KFOmCnqEu92Fr1Mu4mxKKTU1Kg.woff2
null /s/roboto/v18/KFOmCnqEu92Fr1Mu5mxKKTU1Kvnz.woff2
null /s/googlesans/v14/4UaGrENHsxJlGDuGo1OIlL3Owp5eKQtG.woff2
null /accounts/static/_/js/k=gaia.gaiafe_glif.en.ULVZ6XuenpA.O/am=740fMHAHRAIHHAACzAAAAAAAAACAAOABxL55UL1W8ZaLAQ/d=0/ct=zgms/rs=ABkqax0TQFzjaCyaVm2rUjd43zEzc3uuAg/m=NpD4ec,SF3gsd,rHjpXd,pB6Zqd,o02Jie,QLpTOd,oWOlDb,n73qwf,MpJwZc,bIf8i,omf1Od,zbML3c,zy0vNb,uhxrz,otPmVb,rlNAl
null /accounts/static/_/js/k=gaia.gaiafe_glif.en.ULVZ6XuenpA.O/am=740fMHAHRAIHHAACzAAAAAAAAACAAOABxL55UL1W8ZaLAQ/d=0/ct=zgms/rs=ABkqax0TQFzjaCyaVm2rUjd43zEzc3uuAg/m=QOLEBb
null /accounts/static/_/js/k=gaia.gaiafe_glif.en.ULVZ6XuenpA.O/am=740fMHAHRAIHHAACzAAAAAAAAACAAOABxL55UL1W8ZaLAQ/d=0/ct=zgms/rs=ABkqax0TQFzjaCyaVm2rUjd43zEzc3uuAg/m=syk,i5dxUd,RAnnUd,syh,syi,uu7UOe,soHxf
null /s/roboto/v18/KFOlCnqEu92Fr1MmSU5fBBc4AMP6lQ.woff2
POST /_/common/diagnostics/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded;charset=utf-8
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate, br
Host: accounts.google.com
Origin: https://accounts.google.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15
Referer: https://accounts.google.com/signin/oauth/consent?authuser=0&part=AJi8hAMmyuweinQTgiP7BEFWIkdW68TOfQR44wU4f5Fsrjh4eNrIiis2_j-xMolHwNmev4iPf7XEWzD8Xa1luznmElieMwHJ6yXncc9ehd0urZn2dY5fE8DBw-dvF357NCr1gf_B9DEftO5F53OStS8qXUf1mfx4kIRoDxXpMr-OctzUd5J4U8zQFCKMZZQk-dXss1wvsq3TkRxxaQxiVzVuHXDJXZFxtjnNudn1VQMg4cq7j3jzVqBQ6TcZLm4qTeGWT3vDdcgzHY_HVspIBdqcC9ZS_X7w9_UWmGdX9rMPkFmHq2JiIiVrAviolxstU66Yr2x5CrSopbbz5zUKlhFle8FdVHR5l2g0yoLLbwrSohxy6WIFPtXzZkq2yhHLcj1Y8qfYeJgvbbSZKlt_CW39G21NOXQRfgaqSBf3BuX2a1oZCEmRdkWBREasBebsrEcdLTQJhMuF8fF8MODOFU539Y5Y2K4E3TjeDDXycWQUcBmCM0Ev6F6e3Fd5ELF1VqGfHnTDBpwuWmMMri-c7UUWubAxMUiXesvh7dSXDGcu74EkokMTE2psRbThvOAg6RPXREFRVUp2&hl=en&as=S805822397%3A1600768449645120
Content-Length: 1316
Connection: keep-alive
Cookie: SIDCC=AJi4QfFWzSKFBnu1BSnY9ti0wsMZQC9fDv8sOj67LcTaD7GNHS26IZbpJRIjGOcot-zPkTnSxg; __Secure-3PSIDCC=AJi4QfEW3YFwjNvSql-YDSNA1Tc-38OjZEijjFixphNSRtjYw4tkp8cgdQusQUpnouaa8M9ru6M; LSOLH=AH+1Ng09FMQ4lkIxzkftUJ38VXDpJKC/A+EemF9cIitLInrSPNWuIcbFMYKiIwsuU33WlngVePuTup7bKzxY22aPmyK1DVbrczEhegt1bv8WMgcnAV26lZLevM96lbjtFXTfSX8lvQ8T3RxaZxZfYyDNCLG2znialQ==; __Host-GAPS=1:G8F6cX9gMtS3LdxFPTejT1yl0KnA0pnm39JVdbStsHMU1z9vG8NZxG7hRkf5z0J9vb4wJQdEAoCTAEknJ6_xwrDCwqgFhg:KrXT5QwGQ-pKVmxy; NID=204=kcSm67KixgucLyTWTSh6xT3h_6l3ai-bZr6Kw1HYmhFfsjeVBmxYZY7xJCt1yBlyypp41GbAeDEmCn1kr1paE_LbCpYlzlluyaci8QAx8QrVBy4hUvrgduh4g0FE4N3CBtsxEMMSngIcf4Qj-BUVQnOSEV9TP9OpHpCiuwiNsBTDVUPuzrzEb-oSfMvvZDNERdDVJvA7bzic28WZc5NYaIql-O3c3DDr6BjAjV6QPrh9drQsErmG3Zg; APISID=BfFEmMU_MvWZGgmT/AOehMBI5VfeHcceK3; HSID=ACrvojITwMhC_VGRh; SAPISID=f-aPiBcPWX_qWYwZ/AlWDzkIeyLWApe4ib; SID=1QenX-SWpipKyPFufs-xZOVIsftSy2qBttskCyjHyMkLjh64cfkUtjJbDRuE7OGfjzzFOA.; SSID=A9cAEXXD-2lQDUM6H; __Secure-3PAPISID=f-aPiBcPWX_qWYwZ/AlWDzkIeyLWApe4ib; __Secure-3PSID=1QenX-SWpipKyPFufs-xZOVIsftSy2qBttskCyjHyMkLjh64Cm5S_nqG5Krewj1efHpn9w.; LSID=lso|s.NL|s.youtube:1QenX3r2US2YauknKQx5D4lUBX62L7H2yicfnfSJQnbtIQDir995XwVYXbSNayK6HAHqUg.; __Host-3PLSID=lso|s.NL|s.youtube:1QenX3r2US2YauknKQx5D4lUBX62L7H2yicfnfSJQnbtIQDi42b9LQuObqNiLLnZnnh20g.; 1P_JAR=2020-08-31-13; ACCOUNT_CHOOSER=AFx_qI4z1LvDolWgRnAEY743k_x7tOkd19LnM3nSOlfwICQuRAKsHlNJKol5c99RFTHQuiAddtbDVZcVtZTgil8BWAAj8qh3FqoJFXEMvhQ6zfwlkmwWC0OOQBZOMygdAx1ceFSvXRdriuVrvzBYz3Xj_rrSlR425w; ANID=AHWqTUmVaau9SjpFBrTyVTrv33aw75v76KvBZIFp9tt99LMw7UJn0BGTHD17kByc; CONSENT=WP.280c62
Google-Accounts-XSRF: 1
X-Same-Domain: 1

Let me know if this is helpful or if you need different info about the network requests.

LucSpan commented 3 years ago

@shcheklein your help would be much appreciated. I have the following client_secrets.json,

{"web":{"client_id":"xxx","project_id":"invoice-creation-290413","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxx","redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"]}}

As said previously, using this I can connect effortless to the Google Drive API via Google's quickstart.py. However, I'd really like to keep using PyDrive2, rewriting the code to work with the quickstart.py set-up would be cumbersome.

shcheklein commented 3 years ago

@LucSpan the problem is that I can't reproduce it on my end, and the information you shared still not complete. To being able to help we need to try those things I mentioned.

  1. I see that you have javascript_origins set to localhost- try to change app settings to get rid of it. Export credentials again.

  2. clean environment- you are getting Please make sure you have the correct access rights and the repository exists.because you don't have SSH access GitHub. Please use regular HTTP clone git clone https://github.com/iterative/PyDrive2.git

  3. try different browser

  4. capture logs via dev tools:

Screen Shot 2020-09-23 at 12 25 38 PM
shcheklein commented 3 years ago

Closing as stale, not able to reproduce this.

GePlusE commented 3 years ago

I have a related issue. I followed the instructions to set up PyDrive2 within an existing project. After installing PyDrive2 and creating the client_secrets.json (which is in the same directory) I tried to test it ran this code

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

after runnig I get this Traceback:

Failed to find "code" in the query parameters of the redirect.
Try command-line authentication
Traceback (most recent call last):
  File "/Users/gepluse/CodeProjects/ComunioBuster/test2.py", line 5, in <module>
    gauth.LocalWebserverAuth()
  File "/Users/gepluse/CodeProjects/ComunioBuster/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 125, in _decorated
    code = decoratee(self, *args, **kwargs)
  File "/Users/gepluse/CodeProjects/ComunioBuster/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 273, in LocalWebserverAuth
    raise AuthenticationError("No code found in redirect")
pydrive2.auth.AuthenticationError: No code found in redirect

I tried @shcheklein solution by testing it within a clean environment but could get it to work either. The problem seemed to be the application type "web". After creating new credentials with an application type "desktop" the authentication process worked with the new environment. Unfortunately, although I use the same credentials in my existing project I still get the same Traceback.

Do you have any ideas why this happens?

EDIT: Now it didn't work in the clean environment either. Is it possible that the authentication process only works one time?