doctor_Nick42 doctor_Nick42 - 4 months ago 24
Python Question

OAuth 2.0 for Server to Server Applications using Python 3.4, cannot import name 'SERVICE_ACCOUNT'

I'm trying to implement 'Server to Server' OAuth authentication in my Python 3.4 application to work with Google Cloud Storage. So, in general it is described on this page Using OAuth 2.0 for Server to Server Applications

But there's an error while I'm running my script:


Traceback (most recent call last):

File "my_script.py", line 4, in

from oauth2client.service_account import ServiceAccountCredentials

File
"/usr/local/lib/python3.4/dist-packages/oauth2client/service_account.py",
line 31, in
from oauth2client.client import SERVICE_ACCOUNT

ImportError: cannot import name 'SERVICE_ACCOUNT'


So, here's my code:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http

scopes = ['https://www.googleapis.com/auth/cloud-platform.read-only']

credentials = ServiceAccountCredentials.from_json_keyfile_name('./keyfile.json', scopes)

http_auth = credentials.authorize(Http())
service = build('storage', 'v1', http=http_auth)


If you would be so kind to help me with it, it would be great! It's the one of my first scripts on a Python language, and I'm a newbie both in Python and in Linux worlds, so I can miss something very obvious.

If it is important, my OS is Ubuntu 12.04.

Thanks!

Answer

I found that google-api-python-client installed oauth2client version 1.5.2, but it actually needed version 2.0.0. So, your pip install oauth2client --upgrade is what saved you.