CMSiMDE 50833102網際內容管理系統

  • Home
    • Site Map
    • reveal
    • blog
  • Old Stuff
    • About
    • Develop
  • progress rate
    • W05
    • W06
    • W07(更改網誌)
    • W08
    • W09
    • W10(keys)
    • W11&W12(leo)
    • W13(憑證)
    • W14(更改&編輯Blogger)
    • W15-17(小組研究)
  • 第九週實習
    • 電腦軟硬體簡介
    • 網路架構與設定簡介
  • 我的Blogger
  • 本站神獸
W13(憑證) << Previous Next >> W15-17(小組研究)

W14(更改&編輯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

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

'''
用來測試 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")

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

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

6. 將"postid": "456",改成那篇文章的ID

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

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

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

8. Tools > Go


W13(憑證) << Previous Next >> W15-17(小組研究)

Copyright © All rights reserved | This template is made with by Colorlib