title: Understanding Basic Python Google API Sample
body: <p>I mentioned trying to get google java sample code working in one of my previous questions on StackOverflow, but abandoned trying to do that after realizing how deprecated the samples were. Since I dabbled in python a bit about 4 years ago, I decided that I would take a look at the Google Blogger API for Python.</p>
  
  <p>While most of the API calls make sense, I cannot seem to get this sample to run correctly!</p>
  
  <p>Here is the sample I am trying to run:</p>
  
  <pre><code>#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  #
  # Copyright 2014 Google Inc. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #      http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  """Simple command-line sample for Blogger.
  
  Command-line application that retrieves the users blogs and posts.
  
  Usage:
    $ python blogger.py
  
  You can also get help on all the command-line flags the program understands
  by running:
  
    $ python blogger.py --help
  
  To get detailed log output run:
  
    $ python blogger.py --logging_level=DEBUG
  """
  from __future__ import print_function
  
  __author__ = 'jcgregorio@google.com (Joe Gregorio)'
  
  import sys
  
  from oauth2client import client
  from googleapiclient import sample_tools
  
  
  def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv, 'blogger', 'v3', __doc__, __file__,
        scope='https://www.googleapis.com/auth/blogger')
  
    try:
  
        users = service.users()
  
        # Retrieve this user's profile information
        thisuser = users.get(userId='self').execute()
        print('This user\'s display name is: %s' % thisuser['displayName'])
  
        blogs = service.blogs()
  
        # Retrieve the list of Blogs this user has write privileges on
        thisusersblogs = blogs.listByUser(userId='self').execute()
        for blog in thisusersblogs['items']:
          print('The blog named \'%s\' is at: %s' % (blog['name'], blog['url']))
  
        posts = service.posts()
  
        # List the posts for each blog this user has
        for blog in thisusersblogs['items']:
          print('The posts for %s:' % blog['name'])
          request = posts.list(blogId=blog['id'])
          while request != None:
            posts_doc = request.execute()
            if 'items' in posts_doc and not (posts_doc['items'] is None):
              for post in posts_doc['items']:
                print('  %s (%s)' % (post['title'], post['url']))
            request = posts.list_next(request, posts_doc)
  
    except client.AccessTokenRefreshError:
      print ('The credentials have been revoked or expired, please re-run'
        'the application to re-authorize')
  
  if __name__ == '__main__':
    main(sys.argv)
  </code></pre>
  
  <p>I've run this sample in both PyCharm and the terminal, and while the code compiles and runs (which is more than I could say for the Java sample!) I cannot seem to follow where the sample is getting its information. </p>
  
  <p>The sample requires a client_secrets.json file, which I populated with my client id and client secrets keys obtained from the Google API console, however, I don't see how the sample is supposed to get data on the current blogger user, as there doesn't seem to be an input to select a user, input an email address, or anything of the like. The service apparently obtains the current user, but it doesn't actually appear to do so.</p>
  
  <p>client_secrets.json:</p>
  
  <pre><code>{
    "web": {
      "client_id": "[[INSERT CLIENT ID HERE]]",
      "client_secret": "[[INSERT CLIENT SECRET HERE]]",
      "redirect_uris": [],
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://accounts.google.com/o/oauth2/token"
    }
  }
  </code></pre>
  
  <p>In fact, upon running this code, I receive the following error:</p>
  
  <pre><code>/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /google-api-python-client-master/samples/blogger/blogger.py
  This user's display name is: Unknown
  Traceback (most recent call last):
    File "/google-api-python-client-master/samples/blogger/blogger.py", line 83, in &lt;module&gt;
      main(sys.argv)
    File "/google-api-python-client-master/samples/blogger/blogger.py", line 62, in main
      for blog in thisusersblogs['items']:
  KeyError: 'items'
  
  Process finished with exit code 1
  </code></pre>
  
  <p>If someone could help me understand what I'm missing in my understanding of how this sample works, I would definitely appreciate it. My python is definitely rusty, but I'm hoping playing around with this sample code will help me in using it again.</p>