blob: 53ba0b0a678a8dd72ab52750430b67780fb2764b [file] [log] [blame]
Craig Citro15744b12015-03-02 13:34:32 -08001#!/usr/bin/env python
Joe Gregorio077363d2011-10-31 09:12:56 -04002# -*- coding: utf-8 -*-
3#
Craig Citro751b7fb2014-09-23 11:20:38 -07004# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregorio077363d2011-10-31 09:12:56 -04005#
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"""
INADA Naokie8d87822014-08-20 15:25:24 +090034from __future__ import print_function
Joe Gregorio077363d2011-10-31 09:12:56 -040035
36__author__ = 'jcgregorio@google.com (Joe Gregorio)'
37
Joe Gregorio077363d2011-10-31 09:12:56 -040038import sys
Joe Gregorio077363d2011-10-31 09:12:56 -040039
Joe Gregorio652492b2013-06-30 23:19:25 -040040from oauth2client import client
John Asmuth864311d2014-04-24 15:46:08 -040041from googleapiclient import sample_tools
Joe Gregorio077363d2011-10-31 09:12:56 -040042
43
44def main(argv):
Joe Gregorio652492b2013-06-30 23:19:25 -040045 # Authenticate and construct service.
46 service, flags = sample_tools.init(
seppide952a1c72015-06-23 11:26:31 +020047 argv, 'blogger', 'v3', __doc__, __file__,
Joe Gregorio652492b2013-06-30 23:19:25 -040048 scope='https://www.googleapis.com/auth/blogger')
Joe Gregorio077363d2011-10-31 09:12:56 -040049
Joe Gregorio077363d2011-10-31 09:12:56 -040050 try:
51
52 users = service.users()
53
54 # Retrieve this user's profile information
seppide952a1c72015-06-23 11:26:31 +020055 thisuser = users.get(userId='self').execute()
INADA Naokie8d87822014-08-20 15:25:24 +090056 print('This user\'s display name is: %s' % thisuser['displayName'])
Joe Gregorio077363d2011-10-31 09:12:56 -040057
seppide952a1c72015-06-23 11:26:31 +020058 blogs = service.blogs()
59
Joe Gregorio077363d2011-10-31 09:12:56 -040060 # Retrieve the list of Blogs this user has write privileges on
seppide952a1c72015-06-23 11:26:31 +020061 thisusersblogs = blogs.listByUser(userId='self').execute()
Joe Gregorio077363d2011-10-31 09:12:56 -040062 for blog in thisusersblogs['items']:
INADA Naokie8d87822014-08-20 15:25:24 +090063 print('The blog named \'%s\' is at: %s' % (blog['name'], blog['url']))
Joe Gregorio077363d2011-10-31 09:12:56 -040064
65 posts = service.posts()
66
67 # List the posts for each blog this user has
68 for blog in thisusersblogs['items']:
INADA Naokie8d87822014-08-20 15:25:24 +090069 print('The posts for %s:' % blog['name'])
Joe Gregorio077363d2011-10-31 09:12:56 -040070 request = posts.list(blogId=blog['id'])
71 while request != None:
seppide952a1c72015-06-23 11:26:31 +020072 posts_doc = request.execute()
Joe Gregorio077363d2011-10-31 09:12:56 -040073 if 'items' in posts_doc and not (posts_doc['items'] is None):
74 for post in posts_doc['items']:
INADA Naokie8d87822014-08-20 15:25:24 +090075 print(' %s (%s)' % (post['title'], post['url']))
Joe Gregorio077363d2011-10-31 09:12:56 -040076 request = posts.list_next(request, posts_doc)
77
Joe Gregorio652492b2013-06-30 23:19:25 -040078 except client.AccessTokenRefreshError:
79 print ('The credentials have been revoked or expired, please re-run'
80 'the application to re-authorize')
Joe Gregorio077363d2011-10-31 09:12:56 -040081
82if __name__ == '__main__':
83 main(sys.argv)