Insert a draft blog post using Blogger API v3 with Python

为君一笑 提交于 2020-05-11 07:54:26

问题


I am trying to post an article using Blogger Api v3 client library.

https://developers.google.com/blogger/docs/3.0/libraries

I was able to run this sample application to get my blog name and posts.

https://github.com/google/google-api-python-client/tree/master/samples/blogger

I wrote this code to insert a post as draft, and I was able to create a draft. However, there is no body in it.

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]
        if blog['id'] == '*** my_blog_id ***':
            posts.insert(blogId=blog['id'], body='test post', 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)

Expected Result: 'test post' in the drat post

Actual Result: No body in the draft post


回答1:


you need to pass object in body instead of string just define an object like this

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


来源:https://stackoverflow.com/questions/48527480/insert-a-draft-blog-post-using-blogger-api-v3-with-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!