cycuorg blogger 與 Pelican 網誌

以下的這些瑣碎紀錄源自 2004 年公開推出的 Gmail 與 2006 年推出的 Google Apps for Your Domain (也就是之後的 G Suite 以及現在的 Google Workspace).

因為 cycu.org 就是在當時申請帳號時建立的. 從 Wayback Machine 還能發現, 最早的紀錄是 2003 年 12 月, 採用 Xoops 在 Apache 1.3.28 版的 WWW 伺服器所建立的網站. 使用的電子郵件系統就是 Gmail.

現在的 cycuorg blogger 是採用 yen 在 cycu dot org 網域中的帳號所建立, 主要內容來自 CYCU. Google Workspace 只負責 Gmail 帳號的設置. Blogger 與 Pelican Blog 彼此保持內容同步. Youtube 影片則透過 yencycuorg 在 Gmail 的 channel 上傳. 而 CYCU 使用 Github Pages 建立, 綁定的帳號為 cycuorg.

Blogger Python API

Goggle cloud apis 網站位於 https://console.cloud.google.com/apis/, 若要透過 Python 程式建立 Blogger 網誌,

Credential 選擇 OAuth 2.0 Client IDs type: Desktop secrets 格式: json 必須透過下列程式轉為 token, 才能為 google-api-python-client 模組使用

# get secrets: https://console.developers.google.com
# https://developers.google.com/blogger/docs/3.0/using
# pip install google_auth_oauthlib
# under Mac command + b to execute
import pickle
import os
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request


SCOPES = ['https://www.googleapis.com/auth/blogger', ]

# we check if the file tBo store the credentials exists
if not os.path.exists('./../../yen_cycu_blogger_token.dat'):

    flow = InstalledAppFlow.from_client_secrets_file('./../../yen_cycu_blogger_secrets.json', SCOPES)
    credentials = flow.run_local_server()

    with open('yen_cycu_blogger_token.dat', 'wb') as credentials_dat:
        pickle.dump(credentials, credentials_dat)
else:
    with open('yen_cycu_blogger_token.dat', 'rb') as credentials_dat:
        credentials = pickle.load(credentials_dat)
service = build('blogger', 'v3', credentials=credentials)
g.es(service)

已經將 .json 轉為 token.dat 後, 可以 pickle.load() 取出 credentials, 然後利用 service = build('blogger', 'v3', credentials=credentials) 得到
blogger service 物件.

from markdown import markdown
from oauth2client import client
# pip install oauth2client
import os
# 配合使用 credential token
import pickle
from googleapiclient.discovery import build
# pip install google-api-python-client

os.environ['TZ'] = 'Asia/Taipei'
with open('./../../yen_cycu_blogger_token.dat', 'rb') as credentials_dat:
    credentials = pickle.load(credentials_dat)
service = build('blogger', 'v3', credentials=credentials)
blogs = service.blogs()
# 取得使用者所建立網誌名稱
blogs = blogs.listByUser(userId='self').execute()
# post_id is now blogs["items"][0]["id"]
#blog_id = blogs["items"][0]["id"]
# cycuorg.blogspot.com 的 blog_id
blog_id = "7614197805383305517"
#for blog in blogs['items']:
    #print(blog['name'], blog['url'])
posts = service.posts()