Sunday, January 06, 2019

Blogger post automation via Python

Step 1: Go to https://console.developers.google.com and create a new project

Step 2: Go to API Library and search for Blogger


Step 3: Enable Blogger API v3.



Step 4: Create Credentials.


Step 5: Choose Blogger API v3, Other UI, User data.


Step 6: Create OAuth client ID.


Step 7: Go to the Credentials tab and select OAuth client ID.


Step 8: Select Application type as a Web application.


Step 9: Go to the Credentials page and download the JSON file. Remove Client ID from the downloaded JSON and rename it as client_secrets.json.

Step 10: Create a python file in the same directory of the JSON file.
 
from __future__ import print_function

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()

        blogs = service.blogs()

        # Retrieve the list of Blogs this user has write privileges on
        thisusersblogs = blogs.listByUser(userId='self').execute()

        posts = service.posts()

        blog = thisusersblogs['items'][0]
        body = {
        "kind": "blogger#post",
        "title": "posted via python",
        "content":"<div>hello world test</div>"
        }
        if blog['id'] == '_replace_blog_id_here_':
            posts.insert(blogId=blog['id'], body=body, isDraft=True).execute()

    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)

Step 11: This script will invoke a web page on first run. Login with your blog's account. You will notice a new file created named blogger.dat.



ref. 1: https://stackoverflow.com/questions/48527480/insert-a-draft-blog-post-using-blogger-api-v3-with-python?rq=1
ref. 2: https://developers.google.com/blogger/docs/3.0/using#RetrievingABlog

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.