在过去的几周里,我正在尝试学习一些基本的 API。现在我正在尝试学习博客 API,并且我使用博客 API 成功插入了我的帖子,但是当我尝试使用 Blogger API 上传图片时它不起作用。这是我使用的代码,
from __future__ import print_function
import sys
from oauth2client import client
from googleapiclient import sample_tools
bodys = [{
"kind": "blogger#post",
"id": "2",
"title": "one",
"content": "<div>hello world test</div><img src=''/>",
"images": [
{
"url": "example1.png",
},
],
},{
"kind": "blogger#post",
"id": "1",
"title": "two",
"content": "<div>hello world test</div><img src=''/>",
"images": [
{
"url": "example2.png",
},
],
}]
for body in bodys:
def main(argv):
service, flags = sample_tools.init(
argv, 'blogger', 'v3', __doc__, __file__,
scope='https://www.googleapis.com/auth/blogger')
try:
users = service.users()
thisuser = users.get(userId='self').execute()
blogs = service.blogs()
thisusersblogs = blogs.listByUser(userId='self').execute()
posts = service.posts()
blog = thisusersblogs['items'][0]
if blog['id'] == '######':
posts.insert(blogId=blog['id'], body=body,fetchImages=True, isDraft=False).execute()
except client.AccessTokenRefreshError:
print ('Invalid credentials')
if __name__ == '__main__':
main(sys.argv)