Search code examples
pythongoogle-apigoogle-oauth

Importing google api client libraries to python/flask


I am trying to use python to authenticate Google id tokens for their oauth, as per their docs here. The code to do it seems manageable, but I am not too familiar with setting up libraries in the flask framework (or any python framework for that matter).

All I simply want is these lines to work:

from google.oauth2 import id_token
from google.auth.transport import requests

I have installed them with pip like:

pip install --upgrade google-api-python-client

This on its own does not work. But I saw these notes from Google on their docs:

Because the Python client libraries are not installed in the App Engine Python runtime environment, they must be vendored into your application just like third-party libraries.

It is trying to do this where I am lost.

I have added the file appengine_config.py as noted in their instuctions. Although it says it needs to be in the same directory as a app.yaml file, which I do not have and do not see any information on how to set this up.

In short, am I on the correct path to install these google python libraries? If so, what am I missing? If not, what path should I be taking?

EDIT: It turned out I was NOT using APP Engine, but Google's Docs made it seem like these were steps I needed to take which threw me off the correct (and more simplified) path.


Solution

  • The issue seems to be because you're not using virtualenv. Just follow the instructions below and it'll work.

    1. Install virtualenv using the instructions in this link below and once installed activate it using the source /path/to/your/virtualenv command:

    https://cloud.google.com/python/setup#installing_and_using_virtualenv

    1. Once you're inside your vritualenv follow the instructions in the link below to install the required google oauth libraries:

    https://developers.google.com/api-client-library/python/auth/web-app

    1. Now you can follow the below link for API reference or the link you provided:

    https://google-auth.readthedocs.io/en/latest/#

    Tested the above steps locally using ipython and it's working. Below is the output for the same.

    In [1]: from google.oauth2 import id_token
    
    In [2]: dir(id_token)
    Out[2]: 
    ['_GOOGLE_APIS_CERTS_URL',
     '_GOOGLE_OAUTH2_CERTS_URL',
     '__builtins__',
     '__doc__',
     '__file__',
     '__name__',
     '__package__',
     '_fetch_certs',
     'exceptions',
     'http_client',
     'json',
     'jwt',
     'verify_firebase_token',
     'verify_oauth2_token',
     'verify_token']
    
    In [3]: from google.auth.transport import requests
    
    In [4]: dir(requests)
    Out[4]: 
    ['AuthorizedSession',
     'Request',
     '_LOGGER',
     '_Response',
     '__builtins__',
     '__doc__',
     '__file__',
     '__name__',
     '__package__',
     'absolute_import',
     'exceptions',
     'functools',
     'logging',
     'requests',
     'six',
     'transport']
    
    In [5]: 
    

    `