Import Error: Google Analytics API Authorization

2019-04-06 04:46发布

问题:

I'm trying to run the sample provided here https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py for authorization.

I've noticed from other questions in SO that (ImportError: cannot import name SignedJwtAssertionCredentials) SignedJwtAssertionCredentials has been removed and therefore could not be imported.

So, I started to follow the solutions provided both on the GitHub page (https://github.com/google/oauth2client/issues/401) and StackOverflow. So far, nothing worked, I'm still seeing the same error. Following is my code.

import argparse

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

import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools

And, this is the error I'm receiving on running the above code.

ImportError: cannot import name ServiceAccountCredentials

As I'm a complete newbie in this space, I tried to do this for both versions of OAuth (2.0.0 and 1.5.2). I also tried it after installing pyopenssl, but still failed.

回答1:

It seems oauth2client installation is unsuccessful. Try

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



回答2:

Installing pyopenssl fixed the issue for me:

pip install pyopenssl

Based on this answer.



回答3:

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.

Even if you upgrade to use the latest google-api-python-client... you'll want to make sure any *.pyc files from the old library have been removed.