python - 使用 Blogger API v3 和 Python 插入博客文章草稿

标签 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

我编写了这段代码来插入一个帖子作为草稿,并且我能够创建一个草稿。然而,里面并没有 body 。

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中的'test post'

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

最佳答案

你需要在主体中传递对象而不是字符串,只需像这样定义一个对象

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

关于python - 使用 Blogger API v3 和 Python 插入博客文章草稿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48527480/

相关文章:

javascript - Blogger 最近的帖子控制台错误 : Cannot read property 'title' of undefined

python - 如何在 ipython 配置文件中定义函数?

python - 如何为 Bokeh 网格图设置默认/事件工具?

javascript 代码不适用于博主博客

Javascript/Jquery 重新加载

javascript - 让谷歌美化与动态 View 博客模板一起工作

python - 获取图片-Python-pptx

python - 使 tensorflow 模型中的一个输入保持不变

python - python/pygame 如何以均匀速度随机方向移动 Sprite ?

javascript - 如何防止关闭的 </option> 标签被删除?