• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright 2014 Google Inc. All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18"""Simple command-line sample for Blogger.
19
20Command-line application that retrieves the users blogs and posts.
21
22Usage:
23  $ python blogger.py
24
25You can also get help on all the command-line flags the program understands
26by running:
27
28  $ python blogger.py --help
29
30To get detailed log output run:
31
32  $ python blogger.py --logging_level=DEBUG
33"""
34from __future__ import print_function
35
36__author__ = 'jcgregorio@google.com (Joe Gregorio)'
37
38import sys
39
40from oauth2client import client
41from googleapiclient import sample_tools
42
43
44def main(argv):
45  # Authenticate and construct service.
46  service, flags = sample_tools.init(
47      argv, 'blogger', 'v3', __doc__, __file__,
48      scope='https://www.googleapis.com/auth/blogger')
49
50  try:
51
52      users = service.users()
53
54      # Retrieve this user's profile information
55      thisuser = users.get(userId='self').execute()
56      print('This user\'s display name is: %s' % thisuser['displayName'])
57
58      blogs = service.blogs()
59
60      # Retrieve the list of Blogs this user has write privileges on
61      thisusersblogs = blogs.listByUser(userId='self').execute()
62      for blog in thisusersblogs['items']:
63        print('The blog named \'%s\' is at: %s' % (blog['name'], blog['url']))
64
65      posts = service.posts()
66
67      # List the posts for each blog this user has
68      for blog in thisusersblogs['items']:
69        print('The posts for %s:' % blog['name'])
70        request = posts.list(blogId=blog['id'])
71        while request != None:
72          posts_doc = request.execute()
73          if 'items' in posts_doc and not (posts_doc['items'] is None):
74            for post in posts_doc['items']:
75              print('  %s (%s)' % (post['title'], post['url']))
76          request = posts.list_next(request, posts_doc)
77
78  except client.AccessTokenRefreshError:
79    print ('The credentials have been revoked or expired, please re-run'
80      'the application to re-authorize')
81
82if __name__ == '__main__':
83  main(sys.argv)
84