[Python] How do you successfully use Google's python quickstart API in a Colab environment?

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    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'})

    Continue reading...

Compartilhe esta Página