Getting Started with Google APIs Client Library for Python

317 views
Skip to first unread message

Omar Cervantes

unread,
Apr 6, 2013, 1:36:57 PM4/6/13
to google-analytics...@googlegroups.com
I am a new to this API Google. I have installed:
  1. Download the starter application and unzip it.
  2. Download the client secrets file and overwrite the client_secrets.json file that was included in the application.
But when I run the sample.py 


import gflags
import httplib2
import logging
import os
import pprint
import sys

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run

FLAGS = gflags.FLAGS

CLIENT_SECRETS = 'client_secrets.json'

MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to download the client_secrets.json file
and save it at:

   %s

""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)

FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
    scope=[
    ],
    message=MISSING_CLIENT_SECRETS_MESSAGE)

gflags.DEFINE_enum('logging_level', 'ERROR',
    ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
    'Set the level of logging detail.')

def main(argv):
  try:
    argv = FLAGS(argv)
  except gflags.FlagsError, e:
    print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
    sys.exit(1)

  logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))

  storage = Storage('sample.dat')
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run(FLOW, storage)

  http = httplib2.Http()
  http = credentials.authorize(http)

  service = build('customsearch', 'v1', http=http)

  try:

    print "Success! Now add code here."

  except AccessTokenRefreshError:
    print ("The credentials have been revoked or expired, please re-run"
      "the application to re-authorize")

if __name__ == '__main__':
  main(sys.argv)



I get this error in the Python Shell:

>>> 

Traceback (most recent call last):
  File "C:\Users\Omar Cervantes\Desktop\customsearch-v1-python-cmd-line\sample.py", line 37, in <module>
    import a_module
ImportError: No module named a_module
>>> 

I am using Python 2.7 on Windows 7

Any help would be praised. 

Omar Cervantes

unread,
Apr 6, 2013, 2:24:09 PM4/6/13
to google-analytics...@googlegroups.com
line 26, in <module>
    """ % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS)
NameError: name '__file__' is not defined

Aaron Toledo

unread,
Apr 9, 2013, 10:53:02 AM4/9/13
to google-analytics...@googlegroups.com
I'm not on a Windows machine, so I'm not just guessing here. 

It might be because Python doesn't know where the library is installed? You may have to customize the path information for your specific setup if it was actually installed as a library. If you did this, you can run python, import sys, then sys.path to see your libraries.

If you just unzipped it somewhere, you may need to create an empty file named __init__.py in the same directory. Make sure sample.py, client_secrets.json, and __init__.py are all in the same folder.
Reply all
Reply to author
Forward
0 new messages