googleapis / google-auth-library-python-oauthlib

Apache License 2.0
185 stars 83 forks source link

wsgi_app.last_request_uri has no attribute 'replace' #69

Open antoscha opened 4 years ago

antoscha commented 4 years ago

Hi,

I have an issue running Google API Quickstart:

Traceback (most recent call last):
  File "quickstart.py", line 49, in <module>
    main() 
  File "quickstart.py", line 29, in main
    creds = flow.run_local_server(port=0)
  File "/usr/lib/python3.8/site-packages/google_auth_oauthlib/flow.py", line 458, in run_local_server
    authorization_response = wsgi_app.last_request_uri.replace('http', 'https')
AttributeError: 'NoneType' object has no attribute 'replace'

Am I doing something wrong?

busunkim96 commented 4 years ago

@antoscha Hi, would you be able to answer the following questions so we can better troubleshoot the issue?

antoscha commented 4 years ago

I'm using Python 3.8.1 and python-google-auth-oauthlib 0.4.1 that comes from Arch Linux distribution.

busunkim96 commented 4 years ago

I'm not able to reproduce the error.

Would you mind doing a pip freeze so we can see all the dependencies you have?

If you haven't already, you can try creating a separate virtual environment to run your code.

  1. Create a virtual environment

    python3 -m venv env
  2. Activate the virtual environment

    source env/bin/activate
  3. Install dependencies.

    pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
  4. Run the quickstart.

    python quickstart.py
antoscha commented 4 years ago

Unfortunately, running the code in the virtual environment leads to the same error. I've put a wrong link to Google Drive API in the first message, updated now. pip freeze

aiohttp==3.6.2
apipkg==1.5
appdirs==1.4.3
asn1crypto==1.3.0
async-timeout==3.0.1
atomicwrites==1.3.0
attrs==19.3.0
autopep8==1.4.4
bcrypt==3.1.7
beautifulsoup4==4.8.2
browsermob-proxy==0.8.0
btrfsutil==1.1.1
CacheControl==0.12.6
cachetools==3.1.1
cffi==1.13.2
chardet==3.0.4
Click==7.0
colorama==0.4.3
contextlib2==0.6.0
cryptography==2.8
cssselect==1.1.0
cupshelpers==1.0
distlib==0.3.0
distro==1.4.0
dnslib==0.9.12
dnspython==1.16.0
gitdb2==2.0.6
GitPython==3.0.5
gmpy2==2.1.0b1
google-api-python-client==1.7.11
google-auth==1.7.1
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.4.1
gspread==3.1.0
gunicorn==19.9.0
html5lib==1.0.1
httplib2==0.14.0
idna==2.8
importlib-metadata==0.23
iniconfig==1.0.0
Jinja2==2.10.3
localzone==0.9.5
lockfile==0.12.2
loguru==0.4.0
louis==3.12.0
lxml==4.4.2
MarkupSafe==1.1.1
meson==0.53.0
mock==3.0.5
more-itertools==8.1.0
mpmath==1.1.0
msgpack==0.6.2
multidict==4.6.1
namcap==3.2.10
numpy==1.18.1
oauth2client==4.1.3
oauthlib==3.1.0
ordered-set==3.1.1
packaging==20.0
pandas==0.25.3
paramiko==2.7.1
passlib==1.7.2
pbr==5.4.4
pep517==0.6.0
pluggy==0.13.1
ply==3.11
progress==1.5
pwquality==1.4.2
py==1.8.1
py3dns==3.2.1
pyalpm==0.9.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pybind11==2.4.3
pycairo==1.18.2
pycodestyle==2.5.0
pycparser==2.19
pycryptodome==3.9.4
pycups==1.9.74
pycurl==7.43.0.3
pyelftools==0.25
Pygments==2.5.2
PyGObject==3.34.0
PyNaCl==1.3.0
pyOpenSSL==19.1.0
pyparsing==2.4.6
PyQt5==5.14.1
PyQt5-sip==12.7.0
pytest==5.3.2
python-crontab==2.3.8
python-dateutil==2.8.1
python-libtorrent==1.2.3
pytoml==0.1.21
pytz==2019.3
pyudev==0.21.0.dev20191102
requests==2.22.0
requests-oauthlib==1.3.0
retrying==1.3.3
rsa==4.0
scp==0.13.2
selenium==3.141.0
six==1.13.0
slackclient==2.5.0
smmap2==2.0.5
soupsieve==1.9.5
SQLAlchemy==1.3.12
sympy==1.5.1
team==1.0
uritemplate==3.0.1
urllib3==1.25.7
waitress==1.4.2
wcwidth==0.1.8
webencodings==0.5.1
WebOb==1.8.5
WebTest==2.0.33
xlrd==1.2.0
yarl==1.3.0
zipp==0.6.0
epetros commented 4 years ago

https://github.com/gsuitedevs/python-samples/issues/88

zainsohail88 commented 3 years ago

facing same issue, any update here. tried with Python 3.8.2

zainsohail88 commented 3 years ago

never mind, I ended up switching to Chrome browser from Safari and it worked without any problem

mo22 commented 3 years ago

installing latest httplib2 helped.

ipmcc commented 3 years ago

I'm having this problem too.

pip freeze output:

cachetools==4.2.1
certifi==2020.12.5
chardet==4.0.0
chromedriver==2.24.1
google-api-core==1.26.1
google-api-python-client==2.0.2
google-auth==1.27.1
google-auth-httplib2==0.1.0
google-auth-oauthlib==0.4.3
googleapis-common-protos==1.53.0
httplib2==0.18.1
idna==2.10
numpy==1.20.1
oauthlib==3.1.0
packaging==20.9
protobuf==3.15.5
pyasn1==0.4.8
pyasn1-modules==0.2.8
pyparsing==2.4.7
pytz==2021.1
requests==2.25.1
requests-oauthlib==1.3.0
rsa==4.7.2
scipy==1.6.1
selenium==3.141.0
six==1.15.0
uritemplate==3.0.1
urllib3==1.26.3

Per the above comment from @mo22 I tried rolling back to the version of httplib2 that was current when they posted that comment, but that didn't help.

Like @zainsohail88 using Safari didn't work at all (i.e. using Safari, I never even get to the wsgi_app.last_request_uri.replace error). Chrome at least gets me far enough to hit the error.

FWIW, I was able to get unblocked by using the out-of-band workflow outlined in https://github.com/googleapis/google-auth-library-python-oauthlib/blob/master/google_auth_oauthlib/flow.py That said, for a "quickstart" this wsgi_app.last_request_uri.replace bug sure did slow me down. :)

abeiertz commented 3 years ago

For what it's worth, I was able to reproduce this error by running the quickstart and waiting about 10-15 seconds before clicking allow. This might not be everyone's case here but perhaps it'd help someone else stepping in: did you take a long time before clicking allow?

SamratDe commented 3 years ago

For me, this problem only occurred in Safari. Was not there when I tried Chrome.

NaoyukiTai commented 3 years ago

This is a problem for both Chrome and Safari for me. As a matter of fact, any browser that refuses to use http for oauth2 redirect url doesn't work.

In short, during the oauth2, a web server is running. This temporary web server only supports http and not https.

wsgi_app never gets to see the redirected URL as it is only listening to http - which is where the auth token is.

In other word, web browser was told to redirect to "http://localhost:PORT:/?state=..." but instead, it swaps the http with https and tries to access, so the wsgi_app never gets the URL back, and therefore, the crash happens.

This has nothing to do with the library version or anything. Web browser is not redirecting to the URL that it got from Google.

NaoyukiTai commented 3 years ago

If you are having this problem with Chrome, this is how to make it work. Open the Chrome setting.

chrome://net-internals/#hsts

There is a "Delete domain security policies". Put in "localhost" to Domain: input, and hit Delete. Now, the localhost is excluded from http -> https redirection.

shu-ha-ri commented 3 years ago

If you are having this problem with Chrome, this is how to make it work. Open the Chrome setting.

chrome://net-internals/#hsts

There is a "Delete domain security policies". Put in "localhost" to Domain: input, and hit Delete. Now, the localhost is excluded from http -> https redirection.

This has done the trick for me, thanks!

ohshazbot commented 2 years ago

FWIW, I'm also experiencing this on firefox now. And the sole reason I'm encountering this is due to teh push to remove the oob workflow, which is being deprecated (which is earlier mentioned in this thread as one of the workarounds)

luanldt commented 1 year ago

If you are having this problem with Chrome, this is how to make it work. Open the Chrome setting.

chrome://net-internals/#hsts

There is a "Delete domain security policies". Put in "localhost" to Domain: input, and hit Delete. Now, the localhost is excluded from http -> https redirection.

Awesome tricks, thanks so much 🥰🥰

pembo13 commented 1 year ago

How exactly do I get past this error? The quickstart for Google Sheets API references code that simply doesn't work.

shu-ha-ri commented 1 year ago

How exactly do I get past this error? The quickstart for Google Sheets API references code that simply doesn't work.

Hi @pembo13 , did you see the messages above by NaoyukiTai?

If you are having this problem with Chrome, this is how to make it work. Open the Chrome setting.

chrome://net-internals/#hsts

There is a "Delete domain security policies". Put in "localhost" to Domain: input, and hit Delete. Now, the localhost is excluded from http -> https redirection.

This has worked for me as well as others after me. Give it a shot. Reasoning here is that your localhost is not offering https connections, so needs to be excluded from the redirect int he browser settings.

pembo13 commented 1 year ago

@shu-ha-ri this wasn't it. I was using offline/desktop credentials over SSH. The HTTP local server timing out before I could copy and paste the URL to start the flow. And the server was only binding 127.0.0.1 on the remote machine. I had to fix both these issues to proceed.

run_console would have been much simpler, but it's no more.

jhnlmn commented 6 months ago

Is there a generic solution for this "AttributeError: 'NoneType' object has no attribute 'replace'" ? I am trying to write a setup script, which any of my coworkers can run on their freshly set up Windows to setup our development environment. And the script fails with this this error when I try to access Drive. A clean Windows only has Edge browser. I cannot force coworkers to install Chrome or change it's security preferences. Is there anything I can do to make setup process as automatic as possible without requiring user to do anything?

jhnlmn commented 6 months ago

I found that I fix this by editing C:\Users\me\AppData\Local\Programs\Python\Python312\Lib\site-packages\google_auth_oauthlib\flow.py and replacing local_server.handle_request() by time_start = time.time() local_server.timeout = timeout_seconds local_server.handle_request() while not wsgi_app.last_request_uri: if timeout_seconds and time.time() - time_start > timeout_seconds: raise Exception("Timeout waiting for browser") print("call handle_request again") local_server.handle_request() And now it seem to work OK with Edge 100.0.1185.36 and Chrome 123.0.6312.59 with default settings. It actually takes 3 calls to local_server.handle_request() until it gets valid response from Edge and 2 from Chrome,

So, it works, but it requires changing google_auth_oauthlib source code. What should I do now? Try to patch it locally or is there a way to fix it upstream?