Wednesday, December 23, 2015

Migrating to the new Google Drive API v3

NOTE: The code covered in this and the previous post are also available in a video walkthrough.

Introduction

In a blog post last week, we introduced readers to performing uploads and downloads files to/from Google Drive from a simple Python command-line script. In an official Google blog post later that same day, the Google Drive API team announced a new version of the API. Great timing huh? Well, good thing I knew it was coming, so that I could prepare this post for you, which is a primer on how to migrate from the current version of the API (v2) to the new one (v3).

As stated by the Drive team, v2 isn't being deprecated, and there are no new features in v3, thus migration isn't required. The new version is mainly for new apps/integrations as well as developers with v2 apps who wish to take advantage of the improvements. This post is intended for those in the latter group, covering porting existing apps to v3. Ready? Let's go straight to the action.

Migrating from Google Drive API v2 to v3

Most of this post will be just examining all the "diffs" between the v2 code sample from the previous post (renamed from drive_updown.py to drive_updown2.py) and its v3 equivalent (drive_updown3.py). We'll take things step-by-step to provide more details, but let's start with all the diffs first:
--- drive_updown2.py 2015-12-10 14:37:11.000000000 -0800
+++ drive_updown3.py 2015-12-10 13:05:07.000000000 -0800
@@ -19,23 +19,24 @@
             'client_secret.json', scope=SCOPES)
     creds = tools.run_flow(flow, store, flags) \
             if flags else tools.run(flow, store)
-DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
+DRIVE = build('drive', 'v3', http=creds.authorize(Http()))
 
 FILES = (
-    ('hello.txt', False),
-    ('hello.txt', True),
+    ('hello.txt', None),
+    ('hello.txt', 'application/vnd.google-apps.document'),
 )
 
-for filename, convert in FILES:
-    metadata = {'title': filename}
-    res = DRIVE.files().insert(convert=convert, body=metadata,
-            media_body=filename, fields='mimeType,exportLinks').execute()
+for filename, mimeType in FILES:
+    metadata = {'name': filename}
+    if mimeType:
+        metadata['mimeType'] = mimeType
+    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
     if res:
         print('Uploaded "%s" (%s)' % (filename, res['mimeType']))
 
 if res:
     MIMETYPE = 'application/pdf'
-    res, data = DRIVE._http.request(res['exportLinks'][MIMETYPE])
+    data = DRIVE.files().export(fileId=res['id'], mimeType=MIMETYPE).execute()
     if data:
         fn = '%s.pdf' % os.path.splitext(filename)[0]
         with open(fn, 'wb') as fh:
We'll start with the building of the service endpoint, with the trivial change of the API version string from 'v2' to 'v3':
-DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
+DRIVE = build('drive', 'v3', http=creds.authorize(Http()))
The next change is the deprecation of the conversion flag. The problem with a Boolean variable is that it limits the possible types of file formats supported. By changing it to a file mimeType instead, the horizons are broadened:
 FILES = (
-    ('hello.txt', False),
-    ('hello.txt', True),
+    ('hello.txt', None),
+    ('hello.txt', 'application/vnd.google-apps.document'),
 )
Your next question will be: "What are the mimeTypes for the supported Google Apps document formats?" The answers can be found at this page in the official docs. This changes the datatype in our array of 2-tuples, so we need to change the loop variable to reflect this... we'll use the mimeType instead of a conversion flag:
-for filename, convert in FILES:
+for filename, mimeType in FILES:
Another change related to deprecating the convert flag is that the mimeType isn't a parameter to the API call. Instead, it's another piece of metadata, so we need to add mimeType to the metadata object.

Related to this is a name change: since a file's name is its name and not its title, it makes more sense to use "name" as the metadata value:
-    metadata = {'title': filename}
+    metadata = {'name': filename}
+    if mimeType:
+        metadata['mimeType'] = mimeType
Why the if statement? Not only did v3 see a change to using mimeTypes, but rather than being a parameter like the conversion flag in v2, the mimeType has been moved into the file's metadata, so if we're doing any conversion, we need to add it to our metadata field (then remove the convert parameter down below).

Next is yet another name change: when creating files on Google Drive, "create()" makes more sense as a method name than "insert()". Reducing the size of payload is another key ingredient of v3. We mentioned in the previous post that insert() returns more than 30 fields in the response payload unless you use the fields parameter to specify exactly which you wish returned. In v3, the default response payload only returns four fields, including all the ones we need in this script, so use of the fields parameter isn't required any more:
-    res = DRIVE.files().insert(convert=convert, body=metadata,
-            media_body=filename, fields='mimeType,exportLinks').execute()
+    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
The final improvement we can demonstrate: users no longer have to make an authorized HTTP GET request with a link to export and download a file in an alternate format like PDF®. Instead, it's now a "normal" API call (to the new "export()" method) with the mimeType as a parameter. The only other parameter you need is the file ID, which comes back as part of the (default) response payload when the create() call was made:
-    res, data = DRIVE._http.request(res['exportLinks'][MIMETYPE])
+    data = DRIVE.files().export(fileId=res['id'], mimeType=MIMETYPE).execute()
That's it! If you run the script, grant the script access to your Google Drive (via the OAuth2 prompt that pops up in the browser), and then you should get output that looks like this:
$ python drive_updown3.py # or python3
Uploaded "hello.txt" (text/plain)
Uploaded "hello.txt" (application/vnd.google-apps.document)
Downloaded "hello.pdf" (application/pdf)

Conclusion

The entire v2 script (drive_updown2.py) was spelled out in full in the previous post, and it hasn't changed since then. Below is the v3 script (drive_updown3.py) for your convenience which runs on both Python 2 and Python 3 (unmodified!):
#!/usr/bin/env python

from __future__ import print_function
import os

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags) \
            if flags else tools.run(flow, store)
DRIVE = build('drive', 'v3', http=creds.authorize(Http()))

FILES = (
    ('hello.txt', None),
    ('hello.txt', 'application/vnd.google-apps.document'),
)

for filename, mimeType in FILES:
    metadata = {'name': filename}
    if mimeType:
        metadata['mimeType'] = mimeType
    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
    if res:
        print('Uploaded "%s" (%s)' % (filename, res['mimeType']))

if res:
    MIMETYPE = 'application/pdf'
    data = DRIVE.files().export(fileId=res['id'], mimeType=MIMETYPE).execute()
    if data:
        fn = '%s.pdf' % os.path.splitext(filename)[0]
        with open(fn, 'wb') as fh:
            fh.write(data)
        print('Downloaded "%s" (%s)' % (fn, MIMETYPE))
)
Just as in the previous post(s), you can now customize this code for your own needs, for a mobile frontend, sysadmin script, or a server-side backend, perhaps accessing other Google APIs. Hope we accomplished our goal by pointing out some of the shortcomings that are in v2 and how they were improved in v3! All of the content in this and the previous post are spelled out visually in this video that I created for you.

Monday, December 14, 2015

Google Drive: Uploading & Downloading files with Python

UPDATE: Since this post was published, the Google Drive team released a newer version of their API. After reading this one, go to the next post to learn about migrating your app from v2 to v3 as well as link to my video which walks through the code samples in both posts.

Introduction

So far in this series of blogposts covering authorized Google APIs, we've used Python to access Google Drive, Gmail, and Google Calendar. Today, we're revisiting Google Drive with a small snippet that uploads plain text files to Drive, with & without conversion to a Google Apps format (Google Docs), then exports & downloads the converted one as PDF®.

Earlier posts demonstrated the structure and "how-to" use Google APIs in general, so more recent posts, including this one, focus on solutions and apps, and use of specific APIs. Once you review the earlier material, you're ready to start with authorization scopes then see how to use the API itself.

    Google Drive API Scopes

    Google Drive features numerous API scopes of authorization. As usual, we always recommend you use the most restrictive scope possible that allows your app to do its work. You'll request fewer permissions from your users (which makes them happier), and it also makes your app more secure, possibly preventing modifying, destroying, or corrupting data, or perhaps inadvertently going over quotas. Since we need to upload/create files in Google Drive, the minimum scope we need is:
    • 'https://www.googleapis.com/auth/drive.file' — Per-file create/open, read/write access

    Using the Google Drive API

    Let's get going with our example today that uploads and downloads a simple plain text file to Drive. The file will be uploaded twice, once as-is, and the second time, converted to a Google Docs document. The last part of the script will request an export of the (uploaded) Google Doc as PDF and download that from Drive.

    Since we've fully covered the authorization boilerplate fully in earlier posts and videos, we're going to skip that here and jump right to the action, creating of a service endpoint to Drive. The API name is (of course) 'drive', and the current version of the API is 2, so use the string 'v2' in this call to the apiclient.discovey.build() function:

    DRIVE = build('drive', 'v2', http=creds.authorize(Http()))

    Let's also create a FILES array object (tuple, list, etc.) which holds 2-tuples of the files to upload. These pairs are made up of a filename and a flag indicating whether or not you wish the file to be converted to a Google Apps format:
    FILES = (
        ('hello.txt', False),
        ('hello.txt', True),
    )
    Since we're uploading a plain text file, a conversion to Apps format means Google Docs. (You can imagine that if it was a CSV file, the target format would be Google Sheets instead.) With the setup complete, let's move on to the code that performs the file uploads.

    We'll loop through FILES, cycling through each file-convert flag pair and call the files.insert() method to perform the upload. The four parameters needed are: 1) the conversion flag, 2) the file metadata, which is only the filename (see below), 3) the media_body, which is also the filename but has a different purpose — it specifies where the file content will come from, meaning the file will be opened and its data transferred to the API, and 4), a set of fields you want returned.
    for filename, convert in FILES:
        metadata = {'title': filename}
        res = DRIVE.files().insert(convert=convert, body=metadata,
                media_body=filename, fields='mimeType,exportLinks').execute()
        if res:
            print('Uploaded "%s" (%s)' % (filename, res['mimeType']))
    
    It's important to give the fields() parameter because if you don't, more than 30(!) are returned by default from the API. There's no need to waste all that network traffic if all you need are just a couple. In our case, we only want the mimeType, to confirm what the file was saved as, and exportLinks, which we'll explore in a moment. If files are uploaded successfully, the print() lets the user know, and then we move on to the final section of the script.

    Before we dig into the last bit of code, it's important to realize that the res variable still contains the result from the second upload, the one where the file is converted to Google Docs. This is important because this is where we need to extract the download link for the format you want (res['exportLinks'][MIMETYPE]). The way to download the file is to make an authorized HTTP GET call, passing in that link. In our case, it's the PDF version. If the download is successful, the data variable will have the payload to write to disk. If all's good, let the user know:
    if res:
        MIMETYPE = 'application/pdf'
        res, data = DRIVE._http.request(res['exportLinks'][MIMETYPE])
        if data:
            fn = '%s.pdf' % os.path.splitext(filename)[0]
            with open(fn, 'wb') as fh:
                fh.write(data)
            print('Downloaded "%s" (%s)' % (fn, MIMETYPE))
    
    Final note: this code sample is slightly different from previous posts in two big ways: 1) now that the Google APIs Client Library runs on Python 3, I'll try to produce only code samples for this blog that run unmodified under both 2.x and 3.x interpreters — the primary one-line difference being the import of the print() function, and 2) we're going to incorporate the use of the run_flow() function from oauth2client.tools and only fallback to the deprecated run() function if necessary — more info on this change available in this earlier post.

    If you run the script, grant the script access to your Google Drive (via the OAuth2 prompt that pops up in the browser), and then you should get output that looks like this:
    $ python drive_updown3.py # or python3
    Uploaded "hello.txt" (text/plain)
    Uploaded "hello.txt" (application/vnd.google-apps.document)
    Downloaded "hello.pdf" (application/pdf)
    

    Conclusion

    Below is the entire script for your convenience which runs on both Python 2 and Python 3 (unmodified!):
    #!/usr/bin/env python
    
    from __future__ import print_function
    import os
    
    from apiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    try:
        import argparse
        flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
    except ImportError:
        flags = None
    
    SCOPES = 'https://www.googleapis.com/auth/drive.file'
    store = file.Storage('storage.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
        creds = tools.run_flow(flow, store, flags) \
                if flags else tools.run(flow, store)
    DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
    
    FILES = (
        ('hello.txt', False),
        ('hello.txt', True),
    )
    
    for filename, convert in FILES:
        metadata = {'title': filename}
        res = DRIVE.files().insert(convert=convert, body=metadata,
                media_body=filename, fields='mimeType,exportLinks').execute()
        if res:
            print('Uploaded "%s" (%s)' % (filename, res['mimeType']))
    
    if res:
        MIMETYPE = 'application/pdf'
        res, data = DRIVE._http.request(res['exportLinks'][MIMETYPE])
        if data:
            fn = '%s.pdf' % os.path.splitext(filename)[0]
            with open(fn, 'wb') as fh:
                fh.write(data)
            print('Downloaded "%s" (%s)' % (fn, MIMETYPE))
    
    You can now customize this code for your own needs, for a mobile frontend, sysadmin script, or a server-side backend, perhaps accessing other Google APIs. If you want to see another example of using the Drive API, check out this earlier post listing the files in Google Drive and its accompanying video as well as a similar example in the official docs or its equivalent in Java (server-side, Android), iOS (Objective-C, Swift), C#/.NET, PHP, Ruby, JavaScript (client-side, Node.js, Google Apps Script), or Go. That's it... hope you find these code samples useful in helping you get started with the Drive API!

    UPDATE: Since this post was published, the Google Drive team released a newer version of their API. Go to the next post to learn about migrating your app from v2 to v3 as well as link to my video which walks through the code samples in both posts.

    EXTRA CREDIT: Feel free to experiment and try something else to test your skills and challenge yourself as there's a lot more to Drive than just uploading and downloading files. Experiment with creating folders and manipulate files there, work with a folder of photos and organize them using the image metadata available to you, implement a search engine for your Drive files, etc. There are so many things you can do! 

    Wednesday, September 9, 2015

    Creating events in Google Calendar from Python

    NOTE: The code covered in this blogpost is also available in a video walkthrough here.

    UPDATE (Jan 2016): Tweaked the code to support oauth2client.tools.run_flow() which deprecates oauth2client.tools.run(). You can read more about that change and migration steps here.

    Introduction

    So far in this series of blogposts covering authorized Google APIs, we've used Python code to access Google Drive and Gmail. Today, we're going to demonstrate the Google Calendar API. While Google Calendar, and calendaring in general, have been around for a long time and are fairly stable, it's somewhat of a mystery as to why so few developers create good calendar integrations, whether it be with Google Calendar, or other systems. We'll try to show it isn't necessarily difficult and hopefully motivate some of you out there to add a calendaring feature in your next mobile or web app.

    Earlier posts demonstrated the structure and "how-to" use Google APIs in general, so more recent posts, including this one, focus on solutions and apps, and use of specific APIs. Once you review the earlier material, you're ready to start with authorization scopes then see how to use the API itself.

      Google Calendar API Scopes

      Below are the Google Calendar API scopes of authorization. There are only a pair (at the time of this writing): read-only and read/write. As usual, use the most restrictive scope you possibly can yet still allowing your app to do its work. This makes your app more secure and may prevent inadvertently going over any quotas, or accessing, destroying, or corrupting data. Also, users are less hesitant to install your app if it asks only for more restricted access to their calendars. However, it's likely that in order to really use the API to its fullest, you will probably have to ask for read-write so that you can add, update, or delete events in their calendars.
      • 'https://www.googleapis.com/auth/calendar.readonly' — Read-only access to calendar
      • 'https://www.googleapis.com/auth/calendar' — Read/write access to calendar

      Using the Google Calendar API

      We're going to create a sample Python script that inserts a new event into your Google Calendar. Since this requires modifying your calendar, you need the read/write scope above. The API name is 'calendar' which is currently on version 3, so here's the call to apiclient.discovery.build() you'll use:

      CAL = build('calendar', 'v3', http=creds.authorize(Http()))

      Note that all lines of code above that is predominantly boilerplate (that was explained in earlier posts and videos). Anyway, we've got an established service endpoint with build(), we need to come up with the data to create a calendar event with, at the very least, an event name plus start and end times.

      Timezone required (and in a specific format)

      The API requires a GMT offset, the number of hours your timezone is away from Coordinated Universal Time (UTC, more commonly known as GMT). The format is +/-HH:MM away from UTC. For example, Pacific Daylight Time (PDT, also known as Mountain Standard Time, or MST), is "-07:00," or seven hours behind UTC while Nepal Standard Time (NST [or NPT to avoid confusion with Newfoundland Standard Time]), is "+05:45," or five hours and forty-five minutes ahead of UTC. Also, the timezone must be in RFC 3339 format, which implements the specifications of ISO 8601 for the Internet. Timestamps look like the following in the required format: "YYYY-MM-DDTHH:MM:SS±HH:MM". For example, September 15, 2015 at 7 PM PDT is represented by this string: "2015-09-15T19:00:00-07:00".

      The sample script uses the PDT timezone, so we set the GMT_OFF variable to "-07:00". The EVENT body will hold the event name, and start and end times suffixed with the GMT offset:
      GMT_OFF = '-07:00'    # PDT/MST/GMT-7
      EVENT = {
          'summary': 'Dinner with friends',
          'start':   {'dateTime': '2015-09-15T19:00:00%s' % GMT_OFF},
          'end':     {'dateTime': '2015-09-15T22:00:00%s' % GMT_OFF},
      }
      Use the insert() method of the events() service to add the event. As expected, one required parameter is the ID of the calendar to insert the event into. A special value of 'primary' has been set aside for the currently authenticated user. The other required parameter is the event body. In our request, we also ask the Calendar API to send email notifications to the guests, and that's done by passing in the sendNotifications flag with a True value. Our call to the API looks like this:
      e = CAL.events().insert(calendarId='primary',
          sendNotifications=True, body=EVENT).execute()
      The one remaining thing is to confirm that the calendar event was created successfully. We do that by checking the return value — it should be an Event object with all the details we passed in a moment ago:
      print('''*** %r event added:
          Start: %s
          End:   %s''' % (e['summary'].encode('utf-8'),
              e['start']['dateTime'], e['end']['dateTime']))
      
      Now, if you really want some proof the event was created, one of the fields that's created is a link to the calendar event. We don't use it in the code, but you can... just use e['htmlLink'].

      Regardless, that's pretty much the entire script save for the OAuth2 code that we're so familiar with from previous posts. The script is posted below in its entirety, and if you run it, depending on the date/times you use, you'll see something like this:
      $ python gcal_insert.py
      *** 'Dinner with friends' event added:
          Start: 2015-09-15T19:00:00-07:00
          End:   2015-09-15T22:00:00-07:00
      It also works with Python 3 with one slight nit/difference being the "b" prefix on from the event name due to converting from Unicode to bytes:
      *** b'Dinner with friends' event added:

      Conclusion

      There can be much more to adding a calendar event, such as events that repeat with a recurrence rule, the ability to add attachments for an event, such as a party invitation or a PDF of the show tickets. For more on what you can do when creating events, take a look at the docs for events().insert() as well as the corresponding developer guide. All of the docs for the Google Calendar API can be found here. Also be sure to check out the companion video for this code sample. That's it!

      Below is the entire script for your convenience which runs on both Python 2 and Python 3 (unmodified!):
      from __future__ import print_function
      from apiclient.discovery import build
      from httplib2 import Http
      from oauth2client import file, client, tools
      
      try:
          import argparse
          flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
      except ImportError:
          flags = None
      
      SCOPES = 'https://www.googleapis.com/auth/calendar'
      store = file.Storage('storage.json')
      creds = store.get()
      if not creds or creds.invalid:
          flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
          creds = tools.run_flow(flow, store, flags) \
                  if flags else tools.run(flow, store)
      CAL = build('calendar', 'v3', http=creds.authorize(Http()))
      
      GMT_OFF = '-07:00'      # PDT/MST/GMT-7
      EVENT = {
          'summary': 'Dinner with friends',
          'start':  {'dateTime': '2015-09-15T19:00:00%s' % GMT_OFF},
          'end':    {'dateTime': '2015-09-15T22:00:00%s' % GMT_OFF},
          'attendees': [
              {'email': 'friend1@example.com'},
              {'email': 'friend2@example.com'},
          ],
      }
      
      e = CAL.events().insert(calendarId='primary',
              sendNotifications=True, body=EVENT).execute()
      
      print('''*** %r event added:
          Start: %s
          End:   %s''' % (e['summary'].encode('utf-8'),
              e['start']['dateTime'], e['end']['dateTime']))
      
      You can now customize this code for your own needs, for a mobile frontend, a server-side backend, or to access other Google APIs. If you want to see another example of using the Calendar API (listing the next 10 events in your calendar), check out the Python Quickstart example or its equivalent in Java (server-side, Android), iOS (Objective-C, Swift), C#/.NET, PHP, Ruby, JavaScript (client-side, Node.js), or Go. That's it... hope you find these code samples useful in helping you get started with the Calendar API!

      EXTRA CREDIT: To test your skills and challenge yourself, try creating recurring events (such as when you expect to receive your paycheck), events with attachments, or even editing existing events.

      Thursday, August 6, 2015

      Accessing Gmail from Python (plus BONUS)

      NOTE: The code covered in this blogpost is also available in a video walkthrough here.

      Introduction

      The last several posts have illustrated how to connect to public/simple and authorized Google APIs. Today, we're going to demonstrate accessing the Gmail (another authorized) API. Yes, you read that correctly... "API." In the old days, you access mail services with standard Internet protocols such as IMAP/POP and SMTP. However, while they are standards, they haven't kept up with modern day email usage and developers' needs that go along with it. In comes the Gmail API which provides CRUD access to email threads and drafts along with messages, search queries, management of labels (like folders), and domain administration features that are an extra concern for enterprise developers.

      Earlier posts demonstrate the structure and "how-to" use Google APIs in general, so the most recent posts, including this one, focus on solutions and apps, and use of specific APIs. Once you review the earlier material, you're ready to start with Gmail scopes then see how to use the API itself.

        Gmail API Scopes

        Below are the Gmail API scopes of authorization. We're listing them in most-to-least restrictive order because that's the order you should consider using them in  use the most restrictive scope you possibly can yet still allowing your app to do its work. This makes your app more secure and may prevent inadvertently going over any quotas, or accessing, destroying, or corrupting data. Also, users are less hesitant to install your app if it asks only for more restricted access to their inboxes.
        • 'https://www.googleapis.com/auth/gmail.readonly' — Read-only access to all resources + metadata
        • 'https://www.googleapis.com/auth/gmail.send' — Send messages only (no inbox read nor modify)
        • 'https://www.googleapis.com/auth/gmail.labels' — Create, read, update, and delete labels only
        • 'https://www.googleapis.com/auth/gmail.insert' — Insert and import messages only
        • 'https://www.googleapis.com/auth/gmail.compose' — Create, read, update, delete, and send email drafts and messages
        • 'https://www.googleapis.com/auth/gmail.modify' — All read/write operations except for immediate & permanent deletion of threads & messages
        • 'https://mail.google.com/' — All read/write operations (use with caution)

        Using the Gmail API

        We're going to create a sample Python script that goes through your Gmail threads and looks for those which have more than 2 messages, for example, if you're seeking particularly chatty threads on mailing lists you're subscribed to. Since we're only peeking at inbox content, the only scope we'll request is 'gmail.readonly', the most restrictive scope. The API string is 'gmail' which is currently on version 1, so here's the call to apiclient.discovery.build() you'll use:

        GMAIL = build('gmail', 'v1', http=creds.authorize(Http()))

        Note that all lines of code above that is predominantly boilerplate (that was explained in earlier posts). Anyway, once you have an established service endpoint with build(), you can use the list() method of the threads service to request the file data. The one required parameter is the user's Gmail address. A special value of 'me' has been set aside for the currently authenticated user.
        threads = GMAIL.users().threads().list(userId='me').execute().get('threads', [])
        If all goes well, the (JSON) response payload will (not be empty or missing and) contain a sequence of threads that we can loop over. For each thread, we need to fetch more info, so we issue a second API call for that. Specifically, we care about the number of messages in a thread:
        for thread in threads:
            tdata = GMAIL.users().threads().get(userId='me', id=thread['id']).execute()
            nmsgs = len(tdata['messages'])
        
        We're seeking only all threads more than 2 (that means at least 3) messages, discarding the rest. If a thread meets that criteria, scan the first message and cycle through the email headers looking for the "Subject" line to display to users, skipping the remaining headers as soon as we find one:
            if nmsgs > 2:
                msg = tdata['messages'][0]['payload']
                subject = ''
                for header in msg['headers']:
                    if header['name'] == 'Subject':
                        subject = header['value']
                        break
                if subject:
                    print('%s (%d msgs)' % (subject, nmsgs))
        
        If you're on many mailing lists, this may give you more messages than desired, so feel free to up the threshold from 2 to 50, 100, or whatever makes sense for you. (In that case, you should use a variable.) Regardless, that's pretty much the entire script save for the OAuth2 code that we're so familiar with from previous posts. The script is posted below in its entirety, and if you run it, you'll see an interesting collection of threads... YMMV depending on what messages are in your inbox:
        $ python3 gmail_threads3.py
        [Tutor] About Python Module to Process Bytes (3 msgs)
        Core Python book review update (30 msgs)
        [Tutor] scratching my head (16 msgs)
        [Tutor] for loop for long numbers (10 msgs)
        [Tutor] How to show the listbox from sqlite and make it searchable? (4 msgs)
        [Tutor] find pickle and retrieve saved data (3 msgs)
        

        BONUS: Python 3!

        You may have noticed above that I named the script gmail_threads3.py... why the "3"? Well, as of Mar 2015 (formally in Apr 2015 when the docs were updated), support for Python 3 was added to Google APIs Client Library (3.3+)! This update was a long time coming (relevant GitHub thread), and allows Python 3 developers to write code that accesses Google APIs. If you're already running 3.x, you can use its pip command (pip3) to install the Client Library:

        $ pip3 install -U google-api-python-client

        Because of this, unlike previous blogposts, we're deliberately going to avoid use of the print statement and switch to the print() function instead. If you're still running Python 2, be sure to add the following import so that the code will also run in your 2.x interpreter:

        from __future__ import print_function

        Conclusion

        To find out more about the input parameters as well as all the fields that are in the response, take a look at the docs for threads().list(). For more information on what other operations you can execute with the Gmail API, take a look at the reference docs and check out the companion video for this code sample. That's it!

        Below is the entire script for your convenience which runs on both Python 2 and Python 3 (unmodified!):
        #!/usr/bin/env python
        
        from __future__ import print_function
        from apiclient.discovery import build
        from httplib2 import Http
        from oauth2client import file, client, tools
        
        CLIENT_SECRET = 'client_secret.json'
        SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
        
        store = file.Storage('storage.json')
        creds = store.get()
        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets(CLIENT_SECRET, SCOPES)
            creds = tools.run(flow, store)
        GMAIL = build('gmail', 'v1', http=creds.authorize(Http()))
        
        threads = GMAIL.users().threads().list(userId='me').execute().get('threads', [])
        for thread in threads:
            tdata = GMAIL.users().threads().get(userId='me', id=thread['id']).execute()
            nmsgs = len(tdata['messages'])
        
            if nmsgs > 2:
                msg = tdata['messages'][0]['payload']
                subject = ''
                for header in msg['headers']:
                    if header['name'] == 'Subject':
                        subject = header['value']
                        break
                if subject:
                    print('%s (%d msgs)' % (subject, nmsgs))
        
        You can now customize this code for your own needs, for a mobile frontend, a server-side backend, or to access other Google APIs. If you want to see another example of using the Gmail API (displaying all your inbox labels), check out the Python Quickstart example in the official docs or its equivalent in Java (server-side, Android), iOS (Objective-C, Swift), C#/.NET, PHP, Ruby, JavaScript (client-side, Node.js), or Go. That's it... hope you find these code samples useful in helping you get started with the Gmail API!

        EXTRA CREDIT: To test your skills and challenge yourself, try writing code that allows users to perform a search across their email, or perhaps creating an email draft, adding attachments, then sending them! Note that to prevent spam, there are strict Program Policies that you must abide with... any abuse could rate limit your account or get it shut down. Check out those rules plus other Gmail terms of use here.

        Tuesday, April 7, 2015

        Migrating from tools.run() to tools.run_flow()

        This mini-tutorial slash migration guide slash PSA (public service announcement) is aimed at Python developers using the Google APIs Client Library (to access Google APIs from their Python applications) currently calling oauth2client.tools.run(), and likely getting deprecation warnings and/or considering a migration to oauth2client.tools.run_flow(), its replacement. 

        UPDATE (Jan 2016): The run() function itself was removed from the client library in Aug 2015, so if you're using any release on or after that, any calls to run() from your application code will throw an exception. This blogpost was created for those of you in this situation and need to migrate immediately.

        Prelude

        We're going to continue our look at accessing Google APIs from Python. In addition to the previous pair of posts (http://goo.gl/57Gufk and http://goo.gl/cdm3kZ), as part of my day job, I've been working on corresponding video content which is part of a developer series called the Launchpad Online. The goal of the series is to help introduce or "launch" developers into using Google APIs, dev tools, or specific API features. (The Google Developers Startups team runs the Launchpad bootcamp events featuring this content delivered live to help entrepreneurs get their startup companies off the ground!) Specifically tied to these blogposts, check out episodes 2 (Creating new apps using Google APIs) and 3 (Accessing Google APIs: common code walkthrough).

        Here in this follow-up, we're going to specifically address the sidebar in the previous post, where we bookmarked an item for future discussion (IOW, the future is now): in the oauth2client package, tools.run() has been deprecated by tools.run_flow(). As explained, use of tools.run() is "easier," meaning less code on-screen (or in a blogpost) hence why I've been using it for code samples. But truth be told that it is outdated. Another problem is that tools.run() requires users to install another package (python-gflags), typically with a command like: "pip install -U python-gflags".

        Now it's time to look at tools.run_flow(), so that you can see the better alternative and can code accordingly, even if the code samples in the videos or blogposts use tools.run(). Yes, tools.run_flow() does requires a recent version of Python.

        Command-line argument processing, or "Why argparse?"

        Python has had several modules in the Standard Library that allow developers to process command-line arguments. The original one was getopt which mirrored the getopt() function from C. In Python 2.3, optparse was introduced, featuring more powerful processing capabilities. However, it was deprecated in 2.7 in favor of a similar module, argparse. (To find out more about their similarities, differences and rationale behind developing argparse , see PEP 389 and this argparse docs page.)

        For the purposes of using Google APIs, you're all set if using Python 2.7 as it's included in the Standard Library. Otherwise Python 2.3-2.6 users can install it with: "pip install -U argparse".  NOTE: while argparse is available in 3.x starting with 3.2, the Google APIs Client Library hasn't been ported to 3.x yet at the time of this writing.


        Replacing tools.run() with tools.run_flow()

        Now let's methodically convert the authorized access to Google APIs code from the previous blogpost from using oauth2client.tools.run() to oauth2client.tools.run_flow(). As a courtesy, this is the code I'm talking about that needs the upgrade:

        from apiclient.discovery import build
        from httplib2 import Http
        from oauth2client import file, client, tools

        SCOPES = # 1 or more scopes, i.e., 'https://www.googleapis.com/auth/youtube'
        CLIENT_SECRET_FILE = 'client_secret.json' # downloaded JSON file

        store = file.Storage('storage.json')
        creds = store.get()
        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
            creds = tools.run(flow, store)

        # API information, i.e., (API='youtube', VERSION='v3')
        SERVICE = build(API, VERSION, http=creds.authorize(Http()))

        If we wanted to make it easy, we'd simply direct users to add the requisite import argparse line (after 2.3-2.6 users install it). However, one practice I'm apt to follow, if options avail themselves, is to hedge my bets. Why force users one direction or the other? Why can't we use tools.run_flow() if argparse is available, and fallback to tools.run() otherwise? Rather than a required import, I'll check and set a sentinel like this:

        try:
            import argparse
            flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
        except ImportError:
            flags = None

        If 'flags' comes back as None, we use tools.run() otherwise use tools.run_flow(). The line this affects is the assignment of credentials in the if-block where we have to run through the flow. If argparse is indeed available, 'flags' will hold the ArgumentParser instance registered with the tools.argparser object as a parent. We now only need to use the sentinel to choose which "run" function to run, so to do that, replace this original line of code:

            creds = tools.run(flow, store)

        With:

            if flags:
                creds = tools.run_flow(flow, store, flags)
            else:
                creds = tools.run(flow, store)

        Those obsessed with Python's ternary (? :) operation can do this instead, although it's not as easy to read/understand as the above (and thus, less "Pythonic"):

            creds = tools.run_flow(flow, store, flags) if flags else tools.run(flow, store)

        UPDATE (Dec 2015): The primary reason you need this code is in case argparse isn't available (an older version of Python and one you haven't installed argparse manually for as described above), so that you fallback from run_flow() to run(). If you're using 2.7 or newer, you shouldn't have this problem. (For 3.x, argparse was added in 3.2, but the Google APIs Client Library doesn't support any version older than 3.3, so you should be good there. If you didn't even know Python 3 was supported, then you need to see this post and this Quora Q&A.)

        Conclusion

        That's it, and all the other lines stay the same. The complete updated source is here:

        from apiclient.discovery import build
        from httplib2 import Http
        from oauth2client import file, client, tools

        try:
            import argparse
            flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
        except ImportError:
            flags = None

        SCOPES = # 1 or more scopes, i.e., 'https://www.googleapis.com/auth/youtube'
        CLIENT_SECRET_FILE = 'client_secret.json' # downloaded JSON file

        store = file.Storage('storage.json')
        creds = store.get()
        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
            if flags:
                creds = tools.run_flow(flow, store, flags)
            else:
                creds = tools.run(flow, store)

        # API information, i.e., (API='youtube', VERSION='v3')
        SERVICE = build(API, VERSION, http=creds.authorize(Http()))

        Look for this change to access a variety of Google APIs moving forward with upcoming blogposts and corresponding developer videos, esp. the newest Google Drive post coming up mid-Dec 2015. For now, use this as a guide to modernizing any older code you see in one of my blogposts, videos, or in your own code base. If you want to see some sample usage of the "new way" of doing things, check out the Python Guide for Google Compute Engine.

        Thursday, November 6, 2014

        Authorized Google API access from Python

        NOTE: You can also watch a video walkthrough of the common code covered in this blogpost here.

        Introduction

        In this final installment of a (currently) two-part series introducing Python developers to building on Google APIs, we'll extend from the simple API example from the first post (part 1) just over a month ago. Those first snippets showed some skeleton code and a short real working sample that demonstrate accessing a public (Google) API with an API key (that queried public Google+ posts). An API key however, does not grant applications access to authorized data.

        Authorized data, including user information such as personal files on Google Drive and YouTube playlists, require additional security steps before access is granted. Sharing of and hardcoding credentials such as usernames and passwords is not only insecure, it's also a thing of the past. A more modern approach leverages token exchange, authenticated API calls, and standards such as OAuth2.

        In this post, we'll demonstrate how to use Python to access authorized Google APIs using OAuth2, specifically listing the files (and folders) in your Google Drive. In order to better understand the example, we strongly recommend you check out the OAuth2 guides (general OAuth2 info, OAuth2 as it relates to Python and its client library) in the documentation to get started.

        The docs describe the OAuth2 flow: making a request for authorized access, having the user grant access to your app, and obtaining a(n access) token with which to sign and make authorized API calls with. The steps you need to take to get started begin nearly the same way as for simple API access. The process diverges when you arrive on the Credentials page when following the steps below.

        Google API access

        In order to Google API authorized access, follow these instructions (the first three of which are roughly the same for simple API access):
        • Go to the Google Developers Console and login.
          • Use your Gmail or Google credentials; create an account if needed
        • Click "Create Project" button
          • Enter a Project Name (mutable, human-friendly string only used in the console)
          • Enter a Project ID (immutable, must be unique and not already taken)
        • Once project has been created, click "Enable an API" button
        • Select "Credentials" in left-nav under "APIs & auth"
          • In the top half labeled "OAuth2", click "Create new Client ID"
          • In the new dialog, select your application type — we're building a command-line script which is an "Installed application"
          • In the bottom part of that same dialog, specify the type of installed application; choose "Other" (cmd-line scripts are not web nor mobile)
          • Click "Create Client ID" to generate your credentials
        • Finally, click "Download JSON" to save the new credentials to your computer... perhaps choose a shorter name like "client_secret.json"
        NOTEs: Instructions from the previous blogpost were to get an API key. This time, in the steps above, we're creating and downloading OAuth2 credentials. You can also watch a video walkthrough of this app setup process of getting simple or authorized access credentials in the "DevConsole" here.

          Accessing Google APIs from Python

          In order to access authorized Google APIs from Python, you still need the Google APIs Client Library for Python, so in this case, do follow those installation instructions from part 1.

          We will again use the apiclient.discovery.build() function, which is what we need to create a service endpoint for interacting with an API, authorized or otherwise. However, for authorized data access, we need additional resources, namely the httplib2 and oauth2client packages. Here are the first five lines of the new boilerplate code for authorized access:

          from apiclient.discovery import build
          from httplib2 import Http
          from oauth2client import file, client, tools
          
          CLIENT_SECRET = 'client_secret.json' # downloaded JSON file
          SCOPES = # one or more scopes (strings)
          
          After the imports are some global variables, starting with CLIENT_SECRET. This is the credentials file you saved when you clicked "Download JSON" in the instructions above. SCOPES is a critical variable: it represents the set of scopes of authorization an app wants to obtain (then access) on behalf of user(s). What's does a scope look like?

          Each scope is a single character string, specifically a URL. Here are some examples:
          • 'https://www.googleapis.com/auth/plus.me' — access your personal Google+ settings
          • 'https://www.googleapis.com/auth/drive.metadata.readonly' — read-only access your Google Drive file or folder metadata
          • 'https://www.googleapis.com/auth/youtube' — access your YouTube playlists and other personal information
          You can request one or more scopes, given as a single space-delimited string of scopes or an iterable (list, generator expression, etc.) of strings.  If you were writing an app that accesses both your YouTube playlists as well as your Google+ profile information, your SCOPES variable could be either of the following:
          SCOPES = 'https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/youtube'

          That is space-delimited and made tiny by me so it doesn't wrap in a regular-sized browser window; or it could be an easier-to-read, non-tiny, and non-wrapped tuple:

          SCOPES = (
              'https://www.googleapis.com/auth/plus.me',
              'https://www.googleapis.com/auth/youtube',
          )

          Our example command-line script will just list the files on your Google Drive, so we only need the read-only Drive metadata scope, meaning our SCOPES variable will be just this:
          SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
          The next section of boilerplate represents the security code:
          store = file.Storage('storage.json')
          creds = store.get()
          if not creds or creds.invalid:
              flow = client.flow_from_clientsecrets(CLIENT_SECRET, SCOPES)
              creds = tools.run(flow, store)
          
          Once the user has authorized access to their personal data by your app, a special "access token" is given to your app. This precious resource must be stored somewhere local for the app to use. In our case, we'll store it in a file called "storage.json". The lines setting the store and creds variables are attempting to get a valid access token with which to make an authorized API call.

          If the credentials are missing or invalid, such as being expired, the authorization flow (using the client secret you downloaded along with a set of requested scopes) must be created (by client.flow_from_clientsecrets()) and executed (by tools.run()) to ensure possession of valid credentials. If you don't have credentials at all, the user much explicitly grant permission — I'm sure you've all seen the OAuth2 dialog describing the type of access an app is requesting (remember those scopes?). Once the user clicks "Accept" to grant permission, a valid access token is returned and saved into the storage file (because you passed a handle to it when you called tools.run()).

          Note: tools.run() deprecated by tools.run_flow()
          At the time of this writing, the tools.run() function has been deprecated by tools.run_flow(). We'll explain this in more detail in a future blogpost, but for now, you can use either. The caveats for both: use of tools.run() is "easier" but is outdated and requires another package to download while tools.run_flow() requires more code and a recent version of Python.
          Why is using tools.run() "easier?" Well, it does mean less code, but it also requires the 'gflags' library, so if you need that, install it with "pip install -U python-gflags". The good news with tools.run_flow() is that it does not need this library; the bad news is that you do need to create an argparse.ArgumentParser object (which proxies for the missing 'gflags'), meaning you need Python 2.7. If you wish to do be modern and use tools.run_flow(), read more here in the docs.

          Once the user grants access and valid credentials are saved, you can create one or more endpoints to the secure service(s) desired with apiclient.discovery.build(), just like with simple API access. Its call will look slightly different, mainly that you need to sign your HTTP requests with your credentials rather than passing an API key:

          DRIVE = build(API, VERSION, http=creds.authorize(Http()))

          In our example, we're going to list your files and folders in your Google Drive, so for API, use the string 'drive'. The API version is currently on version 2 so use 'v2' for VERSION:

          DRIVE = build('drive', 'v2', http=creds.authorize(Http()))

          If you want to get comfortable with OAuth2, what it's flow is and how it works, we recommend that you experiment at the OAuth Playground. There you can choose from any number of APIs to access and experience first-hand how your app must be authorized to access personal data.

          Going back to our working example, once you have an established service endpoint, you can use the list() method of the files service to request the file data:

          files = DRIVE.files().list().execute().get('items', [])

          If all goes well, the (JSON) response payload will (not be empty or missing and) contain a sequence of files that we can loop over, displaying file names and types:

          for f in files:
              print f['title'], f['mimeType']

          Just like in the previous blogpost, we're using the print statement here in Python 2, but a pro tip to start getting ready for Python 3 is to add this import to the top of your script (which has no effect in 3.x) so you can use the print() function instead:

          from __future__ import print_function

          Conclusion

          To find out more about the input parameters as well as all the fields that are in the response, take a look at the docs for files().list(). For more information on what other operations you can execute with the Google Drive API, take a look at the reference docs and check out the companion video for this code sample. That's it!

          Below is the entire script for your convenience:
          #!/usr/bin/env python
          
          from apiclient.discovery import build
          from httplib2 import Http
          from oauth2client import file, client, tools
          
          CLIENT_SECRET = 'client_secret.json'
          SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'
          
          store = file.Storage('storage.json')
          creds = store.get()
          if not creds or creds.invalid:
              flow = client.flow_from_clientsecrets(CLIENT_SECRET, SCOPES)
              creds = tools.run(flow, store)
          DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
          
          files = DRIVE.files().list().execute().get('items', [])
          for f in files:
              print f['title'], f['mimeType']
          
          When you run it, you should see pretty much what you'd expect, a list of file or folder names followed by their MIMEtypes — I named my script drive_list.py:
          $ python drive_list.py
          Google Maps demo application/vnd.google-apps.spreadsheet
          Overview of Google APIs - Sep 2014 application/vnd.google-apps.presentation
          tiresResearch.xls application/vnd.google-apps.spreadsheet
          6451_Core_Python_Schedule.doc application/vnd.google-apps.document
          out1.txt application/vnd.google-apps.document
          tiresResearch.xls application/vnd.ms-excel
          6451_Core_Python_Schedule.doc application/msword
          out1.txt text/plain
          Maps and Sheets demo application/vnd.google-apps.spreadsheet
          ProtoRPC Getting Started Guide application/vnd.google-apps.document
          gtaskqueue-1.0.2_public.tar.gz application/x-gzip
          Pull Queues application/vnd.google-apps.folder
          gtaskqueue-1.0.1_public.tar.gz application/x-gzip
          appengine-java-sdk.zip application/zip
          taskqueue.py text/x-python-script
          Google Apps Security Whitepaper 06/10/2010.pdf application/pdf
          
          Obviously your output will be different, depending on what files are in your Google Drive. But that's it... hope this is useful. You can now customize this code for your own needs and/or to access other Google APIs. Thanks for reading!

          EXTRA CREDIT: To test your skills, add functionality to this code that also displays the last modified timestamp, the file (byte)size, and perhaps shave the MIMEtype a bit as it's slightly harder to read in its entirety... perhaps take just the final path element? One last challenge: in the output above, we have both Microsoft Office documents as well as their auto-converted versions for Google Apps... perhaps only show the filename once and have a double-entry for the filetypes!

          Saturday, September 20, 2014

          Simple Google API access from Python

          NOTE: You can also watch a video walkthrough of the common code covered in this blogpost here.

          Introduction

          Back in 2012 when I published Core Python Applications Programming, 3rd ed., I
          posted about how I integrated Google technologies into the book. The only problem is that I presented very specific code for Google App Engine and Google+ only. I didn't show a generic way how, using pretty much the same boilerplate Python snippet, you can access any number of Google APIs; so here we are.

          In this multi-part series, I'll break down the code that allows you to leverage Google APIs to the most basic level (even for Python), so you can customize as necessary for your app, whether it's running as a command-line tool or something server-side in the cloud backending Web or mobile clients. If you've got the book and played around with our Google+ API example, you'll find this code familiar, if not identical — I'll go into more detail here, highlighting the common code for generic API access and then bring in the G+-relevant code later.

          We'll start in this first post by demonstrating how to access public or unauthorized data from Google APIs. (The next post will illustrate how to access authorized data from Google APIs.) Regardless of which you use, the corresponding boilerplate code stands alone. In fact, it's probably best if you saved these generic snippets in a library module so you can (re)use the same bits for any number of apps which access any number of modern Google APIs.

          Google API access

          In order to access Google APIs, follow these instructions:
          • Go to the Google Developers Console and login.
            • Use your Gmail or Google credentials; create an account if needed
          • Click "Create Project" button
            • Enter a Project Name (mutable, human-friendly string only used in the console)
            • Enter a Project ID (immutable, must be unique and not already taken)
          • Once project has been created, click "Enable an API" button
            • You can toggle on any API(s) that support(s) simple API access (not authorized).
            • For the code example below, we use the Google+ API.
            • Other ideas: YouTube Data API, Google Maps API, etc.
            • Find more APIs (and version#s which you need) at the OAuth Playground.
          • Select "Credentials" in left-nav under "APIs & auth"
            • Go to bottom half and click "Create new Key" button
            • Grab long "API KEY" cryptic string and save to Python script
            NOTE: You can also watch a video walkthrough of this app setup process in the "DevConsole" here.

            Accessing Google APIs from Python

            Now that you're set up, everything else is done on the Python side. To talk to a Google API, you need the Google APIs Client Library for Python, specifically the apiclient.discovery.build() function. Download and install the library in your usual way, for example:

            $ pip install -U google-api-python-client
            NOTE: If you're building a Python App Engine app, you'll need something else, the Google APIs Client Library for Python on Google App Engine. It's similar but has extra goodies (specifically decorators — brief generic intro to those in my previous post) just for cloud developers that must be installed elsewhere. As App Engine developers know, libraries must be in the same location on the filesystem as your source code.
            Once everything is installed, make sure that you can import apiclient.discovery:

            $ python
            Python 2.7.6 (default, Apr  9 2014, 11:48:52)
            [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38)] on darwin
            Type "help", "copyright", "credits" or "license" for more information.
            >>> import apiclient.discovery
            >>>

            In discovery.py is the build() function, which is what we need to create a service endpoint for interacting with an API. Now craft the following lines of code in your command-line tool:

            from apiclient.discovery import build

            API_KEY = # copied from project credentials page
            SERVICE = build(API, VERSION, developerKey=API_KEY)

            Take the API key you copied from the credentials page and assign to the API_KEY variable as a string. Obviously, embedding an API key in source code isn't something you'd so in practice as it's not secure whatsoever — stick it in a database, key broker, encrypt, or at least have it in a separate byte code (.pyc/.pyo) file that you import — but we'll allow it now solely for illustrative purposes of a simple command-line script.

            In our short example we're going to do a simple search for "python" in public Google+ posts, so for the API variable, use the string 'plus'. The API version is currently on version 1 (at the time of this writing), so use 'v1' for VERSION. (Each API will use a different name and version string... again, you can find those in the OAuth Playground or in the docs for the specific API you want to use.) Here's the call once we've filled in those variables:

            GPLUS = build('plus', 'v1', developerKey=API_KEY)

            We need a template for the results that come back. There are many fields in a Google+ post, so we're only going to pick three to display... the user name, post timestamp, and a snippet of the post itself:

            TMPL = '''
                User: %s
                Date: %s
                Post: %s
            '''

            Now for the code. Google+ posts are activities (known as "notes;" there are other activities as well). One of the methods you have access to is search(), which lets you query public activities; so that's what we're going to use. Add the following call using the GPLUS service endpoint you already created using the verbs we just described and execute it:

            items = GPLUS.activities().search(query='python').execute().get('items', [])

            If all goes well, the (JSON) response payload will contain a set of 'items' (else we assign an empty list for the for loop). From there, we'll loop through each matching post, do some minor string manipulation to replace all whitespace characters (including NEWLINEs [ \n ]) with spaces, and display if not blank:

            for data in items:
                post = ' '.join(data['title'].strip().split())
                if post:
                    print TMPL % (data['actor']['displayName'],
                                  data['published'], post)

            We're using the print statement here in Python 2, but a pro tip to start getting ready for Python 3 is to add this import to the top of your script (which has no effect in 3.x) so you can use the print() function instead:

            from __future__ import print_function

            Conclusion

            To find out more about the input parameters as well as all the fields that are in the response, take a look at the docs. Below is the entire script missing only the API_KEY which you'll have to fill in yourself.

            #!/usr/bin/env python

            from apiclient.discovery import build

            TMPL = '''
                User: %s
                Date: %s
                Post: %s
            '''

            API_KEY = # copied from project credentials page
            GPLUS = build('plus', 'v1', developerKey=API_KEY)
            items = GPLUS.activities().search(query='python').execute().get('items', [])
            for data in items:
                post = ' '.join(data['title'].strip().split())
                if post:
                    print TMPL % (data['actor']['displayName'],
                                  data['published'], post)

            When you run it, you should see pretty much what you'd expect, a few posts on Python, some on Monty Python, and of course, some on the snake — I called my script plus_search.py:

            $ python plus_search.py 

                User: Jeff Ward
                Date: 2014-09-20T18:08:23.058Z
                Post: How to make python accessible in the command window.


                User: Fayland Lam
                Date: 2014-09-20T16:40:11.512Z
                Post: Data Engineer http://findmjob.com/job/AB7ZKitA5BGYyW1oAlQ0Fw/Data-Engineer.html #python #hadoop #jobs...


                User: Willy's Emporium LTD
                Date: 2014-09-20T16:19:33.851Z
                Post: MONTY PYTHON QUOTES MUG Take a swig to wash down all that albatross and crunchy frog. Featuring 20 ...


                User: Doddy Pal
                Date: 2014-09-20T15:49:54.405Z
                Post: Classic Monty Python!!!


                User: Sebastian Huskins
                Date: 2014-09-20T15:33:00.707Z
                Post: Made a small python script to get shellcode out of an executable. I found a nice commandlinefu.com oneline...

            EXTRA CREDIT: To test your skills, check the docs and add a fourth line to each output which is the URL/link to that specific post, so that you (and your users) can open a browser to it if of interest.

            If you want to build on from here, check out the larger app using the Google+ API featured in Chapter 15 of the book — it adds some brains to this basic code where the Google+ posts are sorted by popularity using a "chatter" score. That just about wraps it up this post. Once you're good to go, then you're ready to learn how to perform authorized Google API access in part 2 of this two-part series!