使用带有Python的Blogger API v3插入博客文章草稿

时间:2018-01-30 17:44:04

标签: python blogger

我正在尝试使用Blogger Api v3客户端库发布文章。

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

我能够运行此示例应用程序来获取我的博客名称和帖子。

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

我写了这段代码来插入一个帖子作为草稿,我能够创建一个草稿。但是,它没有身体。

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)

预期结果:'drat post'中的'测试帖'

实际结果:草稿中没有正文

1 个答案:

答案 0 :(得分:3)

你需要在body中传递object而不是string,只需定义一个像这样的对象

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