使用Blogger API v3和Python插入博客文章草稿 [英] Insert a draft blog post using Blogger API v3 with Python

查看:212
本文介绍了使用Blogger API v3和Python插入博客文章草稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

您需要在对象中传递对象而不是字符串,只需定义一个这样的对象

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

这篇关于使用Blogger API v3和Python插入博客文章草稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆