Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 973
Dmitry Bogomolov Avatar asked Feb 29 '16 10:02

Dmitry Bogomolov


3 Answers

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.

like image 130
tobygriffin Avatar answered Oct 09 '22 21:10

tobygriffin


Well, somehow it started to work. Sorry, I know that self-answering is not very nice, but I spent 2 days with this problem.

So, as I mentioned before, I'm a newbie in a Linux world. I think, oauth2client reinstallation helped me. I tried to downgrade it to 1.5.2 version with pip install -Iv oauth2client==1.5.2, and then after another fail just ran pip install oauth2client --upgrade.

Also I replaced apiclient lib with googleapiclient, so my first string now looking like from googleapiclient.discovery import build

And now it's working, I even don't know why.

Sorry for self-answering again.

like image 35
Dmitry Bogomolov Avatar answered Oct 09 '22 21:10

Dmitry Bogomolov


I had similar issues where I was getting cannot import name xxxx error. It turn out I had old *.pyc files in my environment from an older oauth2client version. Even though I updated to the lastest oauth2client version, the *.pyc files were getting used when I tried to run. I simply deleted the oauth2client *.pyc files and then reran my program without any issues.

like image 1
Steve Scherer Avatar answered Oct 09 '22 23:10

Steve Scherer