This is kind of a follow up to this question I asked 2 years ago. I'm revisiting the work I've done in the past, and trying to get my old scripts to work (... again). And, I'm getting stuck (... again) on simply authenticating into Google. I've successfully failed to get this running both locally on my machine as well as in a Google Colab notebook, with different (very likely basic infra type) errors in each case. I'm happy to make either local or Colab options work (w/ a slight preference for Colab, I think?).
When trying to run through the quickstart guide, I get
>>> from google.oauth2.credentials import Credentials
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
from google.oauth2.credentials import Credentials
ModuleNotFoundError: No module named 'google'
>>> from google_auth_oauthlib.flow import InstalledAppFlow
Traceback (most recent call last):
File "<python-input-9>", line 1, in <module>
from google_auth_oauthlib.flow import InstalledAppFlow
ModuleNotFoundError: No module named 'google_auth_oauthlib'
>>> from googleapiclient.discovery import build
Traceback (most recent call last):
File "<python-input-10>", line 1, in <module>
from googleapiclient.discovery import build
ModuleNotFoundError: No module named 'googleapiclient'
despite having the below with pip freeze
beautifulsoup4==4.11.2
cachetools==5.3.0
certifi==2022.12.7
charset-normalizer==3.1.0
google==3.0.0
google-api-core==2.11.0
google-api-python-client==2.81.0
google-auth==2.16.2
google-auth-httplib2==0.1.0
google-auth-oauthlib==1.0.0
googleapis-common-protos==1.58.0
When trying to run in a Colab environment, I get the following
RefreshError Traceback (most recent call last)
<ipython-input-15-3a42be915110> in <cell line: 0>()
20 , SCOPES
21 )
---> 22 creds.refresh(Request())
23 # If there are no (valid) credentials available, let the user log in.
24 if not creds or not creds.valid:
2 frames
/usr/local/lib/python3.11/dist-packages/google/oauth2/_client.py in _handle_error_response(response_data, retryable_error)
67 error_details = json.dumps(response_data)
68
---> 69 raise exceptions.RefreshError(
70 error_details, response_data, retryable=retryable_error
71 )
RefreshError: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})
This is kind of a follow up to this question I asked 2 years ago. I'm revisiting the work I've done in the past, and trying to get my old scripts to work (... again). And, I'm getting stuck (... again) on simply authenticating into Google. I've successfully failed to get this running both locally on my machine as well as in a Google Colab notebook, with different (very likely basic infra type) errors in each case. I'm happy to make either local or Colab options work (w/ a slight preference for Colab, I think?).
When trying to run through the quickstart guide, I get
>>> from google.oauth2.credentials import Credentials
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
from google.oauth2.credentials import Credentials
ModuleNotFoundError: No module named 'google'
>>> from google_auth_oauthlib.flow import InstalledAppFlow
Traceback (most recent call last):
File "<python-input-9>", line 1, in <module>
from google_auth_oauthlib.flow import InstalledAppFlow
ModuleNotFoundError: No module named 'google_auth_oauthlib'
>>> from googleapiclient.discovery import build
Traceback (most recent call last):
File "<python-input-10>", line 1, in <module>
from googleapiclient.discovery import build
ModuleNotFoundError: No module named 'googleapiclient'
despite having the below with pip freeze
beautifulsoup4==4.11.2
cachetools==5.3.0
certifi==2022.12.7
charset-normalizer==3.1.0
google==3.0.0
google-api-core==2.11.0
google-api-python-client==2.81.0
google-auth==2.16.2
google-auth-httplib2==0.1.0
google-auth-oauthlib==1.0.0
googleapis-common-protos==1.58.0
When trying to run in a Colab environment, I get the following
RefreshError Traceback (most recent call last)
<ipython-input-15-3a42be915110> in <cell line: 0>()
20 , SCOPES
21 )
---> 22 creds.refresh(Request())
23 # If there are no (valid) credentials available, let the user log in.
24 if not creds or not creds.valid:
2 frames
/usr/local/lib/python3.11/dist-packages/google/oauth2/_client.py in _handle_error_response(response_data, retryable_error)
67 error_details = json.dumps(response_data)
68
---> 69 raise exceptions.RefreshError(
70 error_details, response_data, retryable=retryable_error
71 )
RefreshError: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})
Share
Improve this question
asked Mar 12 at 8:12
Scott BordenScott Borden
1831 gold badge3 silver badges14 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 0Answering part of my question after troubleshooting a bunch more!
Locally, it turns out I was in a weird state with where / how packages were installed on my machine. So, despite pip freeze
showing I had installed the required packages, something (what, I don't actually know) was causing python
to not be able to find / source / use them. I was able to resolve this by using venv
to create + source a new virtual environment -> re-import the packages -> ... -> profit?? Here's the specific steps I took, in my working directory:
python -m venv venv
source venv/bin/activate
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Additionally, since it had been a while, I created new google auth credentials (per the quickstart guide), downloaded those to my working directory. Note that because I was in the same working directory as I was in 2023, I renamed my old credentials/token files to 2023_*
so that the auth flow in quickstart (and, my other functions) could read from the new file (which I simply renamed to credentials.json
).
If I find time to debug the Colab flow (it uses the same files / code, simply updated to my Google Drive while using this answer to access them), I'll update my answer here!
pip
may install module for one Python but you may run code with other Python. But Pythons don't share modules. maybe usepython -m pip ...
instead ofpip
. Or checkprint( sys.executable )
to get/full/path/to/python
and use/full/path/to/python -m pip ...
– furas Commented Mar 12 at 9:51gmail-api
(which you show in tags) then it may need to generate special password for access from scripts. It can't use password which you use to login on web page. – furas Commented Mar 12 at 9:56venv
-> reinstalling packages). And, noted on 2 questions and 2 pages - thanks :) – Scott Borden Commented Mar 12 at 15:39