GA Reporting API v3 - "invalid_grant" error after transfer from Python 2.7 to Python 2.6 environment.

122 views
Skip to first unread message

Micah M

unread,
May 22, 2013, 8:18:28 PM5/22/13
to google-analytics...@googlegroups.com
I am getting an "invalid_grant" issue with my Server-to-Server OAuth2.0 and GA Service Account. I can successfully log in and download my data in my virtualenv running python 2.7, but when I ported it to my server on running python 2.6, I get this error. 

Traceback (most recent call last):
  File "ga_test.py", line 48, in <module>
    service = build('analytics','v3',http=http)
  File "/home/mmangione/cron/salesforce/sfdev_env/lib/python2.6/site-packages/oauth2client/util.py", line 128, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/mmangione/cron/salesforce/sfdev_env/lib/python2.6/site-packages/apiclient/discovery.py", line 192, in build
    resp, content = http.request(requested_url)
  File "/home/mmangione/cron/salesforce/sfdev_env/lib/python2.6/site-packages/oauth2client/util.py", line 128, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/mmangione/cron/salesforce/sfdev_env/lib/python2.6/site-packages/oauth2client/client.py", line 475, in new_request
    self._refresh(request_orig)
  File "/home/mmangione/cron/salesforce/sfdev_env/lib/python2.6/site-packages/oauth2client/client.py", line 653, in _refresh
    self._do_refresh_request(http_request)
  File "/home/mmangione/cron/salesforce/sfdev_env/lib/python2.6/site-packages/oauth2client/client.py", line 710, in _do_refresh_request
    raise AccessTokenRefreshError(error_msg)
oauth2client.client.AccessTokenRefreshError: invalid_grant


My main routine is: 

import datetime, httplib2, os, sys, time
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

#=================================================================================
# Step 1. Declare constants, import files and variables.
#=================================================================================
# Dates for use with Google Analytics
today = datetime.datetime.today().strftime('%Y-%m-%d')
yesterday = datetime.date.fromordinal(datetime.date.today().toordinal()-1).strftime('%Y-%m-%d')

# Configure login details for SalesForce
wd = os.path.dirname(os.path.realpath('ga_test.py'))

# Read the GA private key into memory. 
KEY_LOCATION = os.path.join(wd,'privatekey','ga_test.pem')
f = file(KEY_LOCATION, 'rb')
KEY = f.read()
f.close()

#=================================================================================
# Step 2. Login to Google Analytics, retrieve data.
#=================================================================================
LOGIN = '<REMOVED FOR GOOGLE POST>developer.gserviceaccount.com'           # Email from Google API Console.
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'   # Log in to Google Analytics with read-only access.

# Build the Signed JWT Credentials for OAuth2.0
credentials = SignedJwtAssertionCredentials(LOGIN,KEY,SCOPE)
http = httplib2.Http()
http = credentials.authorize(http)

# Build GA service
service = build('analytics','v3',http=http)

The results of pip freeze are:
 
apiclient==1.0.2
argparse==1.2.1
google-api-python-client==1.1
httplib2==0.8
ipython==0.13.2
pyOpenSSL==0.13
pyasn1==0.1.7
pycrypto==2.6
python-gflags==2.0
wsgiref==0.1.2

Hopefully that's enough information. Why would this work on Python 2.7, but not 2.6?

Thanks! 
Reply all
Reply to author
Forward
0 new messages