pythongoogle-oauthgoogle-api-python-client

How do I use google.oauth2 python library?


I'm trying to just make a simple rest call to a secure predict endpoint for a google machine learning project but it can't find the google.oauth2 module. This is my code:

import urllib2
from google.oauth2 import service_account

# Constants
ENDPOINT_URL = 'ml.googleapis.com/v1/projects/{project}/models/{model}:predict?access_token='
SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin']
SERVICE_ACCOUNT_FILE = 'service.json'

credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
access_token=credentials.get_access_token()

opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(ENDPOINT_URL)
request.get_method = lambda: 'POST'
result = opener.open(request).read()
print(str(result))

When I run this I get this error:

Traceback (most recent call last):
  File "wakeUpMLServer.py", line 30, in <module>
    from google.oauth2 import service_account
ImportError: No module named oauth2

I installed the google-api-python-client library using pip (from instructions here: https://developers.google.com/api-client-library/python/apis/oauth2/v1). The install said it was successful. Python version: 2.7.6 Pip version: 1.5.4

In case it is somehow conflicting you should know I also do protobuf file processing on the same server, so I have the protobufs library installed as well. When I do pip list it shows both libraries:

google-api-python-client (1.6.7)
protobuf (3.1.0.post1)

Is there some special way I should be installing the module? How do I verify that the module was installed? Is there any simpler way to just make a simple rest call to a secured ml endpoint that just uses the Rest API and no python?


Solution

  • The google-oauth2 module definitely exists, so maybe it was never installed.

    Do you have the google-auth package installed? Try executing

    pip install --upgrade google-auth

    and then run your code again.