更改&編輯Blogger

接續上週,但有edit功能

blogger

https://2019wcm.blogspot.com/search/label/blogger

影片

https://drive.google.com/file/d/13KBfWIMULL9fAPw-T54cExMt9GUTW4XZ/view

  1. 開啟編輯器預備

  2. 打開黑黑的窗格(我還是習慣這樣叫ww),git submodule:

pip install google-api-python-client

pip install oauth2client

  1. 將下列程式碼打進編輯器:

''' 用來測試 Google Blogger Python API 新增文章程式

pip install google-api-python-client oauth2client

'''

import sys from oauth2client import client from googleapiclient import sample_tools

argv = ""

認證並建立服務

service, flags = sample_tools.init( argv, 'blogger', 'v3', doc, './client_secrets.json', scope='https://www.googleapis.com/auth/blogger')

try: users = service.users() # 取得使用者 profile 資料 user = users.get(userId='self').execute() print('網誌名稱: %s' % user['displayName']) blogs = service.blogs() # 取得使用者所建立網誌名稱 blogs = blogs.listByUser(userId='self').execute() for blog in blogs['items']: print(blog['name'], blog['url']) posts = service.posts() # 新增網誌 post 時, 需要 blog id

body = {
"kind": "blogger#post",
"id": "123",
"title": "透過 Python 程式新增網誌文章1",
"content":"使用 Google Blogger API 可以利用程式新增網誌文章內容1"
}

insert = posts.insert(blogId='123', body=body)
posts_doc = insert.execute()
print(posts_doc)

'''
# 更新網誌文章時的 body
body = {
"kind": "blogger#post",
"title": "透過 Python 程式修改網誌文章2",
"content": "使用 Google Blogger API 可以利用程式修改網誌文章內容. http://mde.tw/cd2019"
}

update = posts.update(blogId="123", postId="456", body=body, publish=True)
update_doc = update.execute()
print(update_doc)
'''

except(client.AccessTokenRefreshError): print("error")

  1. 在tmp存檔,名稱後面加上.py讓它認得出這python

  2. 將"blogid": "123",改成自己的Blogger ID

  3. 將"postid": "456",改成那篇文章的ID (已經被上傳的文章才有)

  4. 依照自己要上傳還是修改,使用 #上傳網誌文章時的 body 或 #更新網誌文章時的 body

"'三引號是多行標註的方法"'

  1. './client_secret.json' ,client_secret.json就是上周做的憑證的檔名,./是指在同一個資料夾

  2. Tools > Go