Here we are going to see the end to end flow of auto post into blogger using python and google blogger API.
For example, if you want to show the currency value
against the $ in your blog, It is hard to update an hour/day wise by
manual. To solve this we can generate the python script to update the
blog information through the google blogger API. We have the many use
case using python automation concept. In fact, We can automate the blog
writing through the below script combined with the content/webs-crawler . The web crawler link is attached here.
It will useful for us to reduce the time consumption and work load.
Over view:
Python code automatically post to blogger and publish the post with out human intervention.Approaches:
- Google oauth2 authentication
- Google blogger V3 API (python client library)
- Python program to automate
Prerequisite:
- Python 2.7, or 3.4 or higher
- Blogger account - Create the blogger account if not through this link
Managed installation
Use pip or setup-tools to manage your installation (you might need to run sudo first).
For pip use this blow command to install the library.
$ pip install --upgrade google-api-python-client
Google Authentication setup
Requests to the Blogger JSON API for public data must be
accompanied by an identifier, which can be an API key or an auth
token.To acquire an API key, visit the APIs Console and follow the below instructions.
Step 1: Here you need to create the project from the top menu. After click the menu it will land the below screen.
Step 2: Click the left side menu and then click APIs & Services menu . In that click the credential option. look at the below image
Step 4: Choose the necessary options from the list. In our example I am going to choose Other. Enter the credential title for future reference.
Step 5: Finally we generated the oauth file. We must download the auth detail to give reference in python programming.
Upto now we finished the authentication setup and install necessary library.
Auto post python script
In below, you may find the python code to create the simple post and publish it into the blogger . The code snippet give below,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #!/usr/bin/env python # coding: utf-8 # ### Python code to auto post to blogger # @author Ramachandran K <ramakavanan@gmail.com> # # In[1]: import sys import os import pickle from oauth2client import client from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request # ##### Adding the blog id and scope of the blogger. # The scope will play the major rolw. It will generate the feasibility to work through the API. # In[3]: BLOG_ID = "xxxxxxxxxxxxxxxxxxxxxxxx" SCOPES = ['https://www.googleapis.com/auth/blogger', 'https://www.googleapis.com/auth/drive.file'] # #### Function to generatet the blogger and drive service # Here we are using the pickle to store and retrive the credential file. The pickle will store the data in bytes. # The normal human cant read that pickle file. # In[4]: def get_blogger_service_obj(): creds = None if os.path.exists('auth_token.pickle'): with open('auth_token.pickle', 'rb') as token: creds = pickle.load(token) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('auth_token.pickle', 'wb') as token: pickle.dump(creds, token) blog_service = build('blogger', 'v3', credentials=creds) drive_service = build('drive', 'v3', credentials=creds) return drive_service,blog_service # In[6]: drive_handler, blog_handler = get_blogger_service_obj() # ### Function to get the blog information # In[7]: def get_blog_information(api_handler=None, blog_max_posts=3): try: if not api_handler: return None blogs = api_handler.blogs() resp = blogs.get(blogId=BLOG_ID, maxPosts=blog_max_posts, view='ADMIN').execute() for blog in resp['posts']['items']: print('The blog title : \'%s\' and url : %s' % (blog['title'], blog['url'])) except Exception as ex: print(str(ex)) # In[8]: get_blog_information(blog_handler) # In[6]: import json # In[9]: data = { 'content': '<b> Welcome ! </b> <br/> This is my first automated post through API.', 'title':'Python post sample', 'labels' : ['First Post'], 'blog': { 'id': BLOG_ID, # The identifier of the Blog that contains this Post. }, } # ### Getting post objects and creating post # In[11]: posts = blog_handler.posts() res = posts.insert(blogId=BLOG_ID, body=data, isDraft=True, fetchImages=True).execute() # #### Printing response from the server # In[13]: res # In[ ]: |
Here we are using the pickle to store and retrieve the confidential information. we have the functions to create the blogger, drive service. I used the blogger to save the image as of now it doesn't need . The post is saved in draft for the above example. Make changes in isDraft as False to publish with in a minute.
Here we are used only blog information and post creation API. To know more API details please visit this link,
For API request and response detail documentation find here.
The above examples are covered only minimal things. To explore more please visit the google official site.
https://developers.google.com/blogger
Thanks for spending your valuable time to read this blog. Please post your feedback, which help me to improve further more.
No comments:
Post a Comment