用python理解Blogger-API的基本原理

2024-05-23 22:26:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着玩Blogger API。文件不清楚,我很难理解如何在博客上发布一些东西。你知道吗

我遇到了以下代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

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",
        "id": "ID-NUMBER",
        "title": "posted via python",
        "content":"<div>hello world test</div>"
        }
        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)

但我不知道我需要在什么地方插入什么信息才能工作。我唯一能理解的是,我需要插入id号"id": "ID-NUMBER"(我在进入blogger时从URL获取)。但是我不明白我需要通过args传递给脚本什么。从文档中,我了解到我需要传递your_client_id your_client_secret,但我似乎无法理解这两个值/对象/文件的确切含义。我还需要插入什么信息?应该用什么来代替__doc__, __file__,?你知道吗


Tags: 文件fromimportclientidexecutemainservice