最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do you successfully use Google's python quickstart API in a Colab environment? - Stack Overflow

programmeradmin0浏览0评论

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
  • 1 maybe you have common problem with installed two Pythons. your pip may install module for one Python but you may run code with other Python. But Pythons don't share modules. maybe use python -m pip ... instead of pip. Or check print( sys.executable ) to get /full/path/to/python and use /full/path/to/python -m pip ... – furas Commented Mar 12 at 9:51
  • 1 you have two different problems and you should rather ask them as separate question on separate pages. – furas Commented Mar 12 at 9:54
  • 1 second poroblem doesn't show which line of code makes problem. Maybe you have to go to Google API and set new credentials and login again in code. If it problem with gmail-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:56
  • Ahh - thanks @furas! I didn't see your comments before posting my answer below. I suspect you're right as it relates to my local machine issues (which, per my answer, I was able to work around via venv -> reinstalling packages). And, noted on 2 questions and 2 pages - thanks :) – Scott Borden Commented Mar 12 at 15:39
  • Re Colab - if I find time, I'll try to work through the new credentials tip! – Scott Borden Commented Mar 12 at 15:40
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Answering 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!

发布评论

评论列表(0)

  1. 暂无评论