2021年5月4日 星期二
2021年4月26日 星期一
Google API - Python 學習筆記 - Upload post
Google Blogger API Table
其實找到好用的 Google API document 就完成 80% 了,這裡紀錄 Python 連結。再紀錄個 Google 給開發者用的 Playground,可以讓你先試試 API。
Post article
這裡紀錄程式碼模擬平常的流程,但其實可以只用 insert 就完成所有事。
-
New Post
新增草稿。
# Create a new draft post. # The return value (newpost) is dict contain all info of this draft newpost = service.posts().insert(blogId=BLOGID, isDraft=True).execute() -
Update content
Update 好 content 後,要轉成 JSON object。以下若程式碼直接傳 JsonPost(str),會回傳錯誤 Invalid JSON payload received. Unknown name “”: Root element must be a message,即使他們 print 出來一模一樣。
# Update some content of the new draft post. newpost['title'] = "posted via python" newpost['content'] = "<div>hello world test</div>" JsonPost = json.dumps(newpost, indent=4, ensure_ascii=False) service.posts().update(blogId=BLOGID, postId=newpost['id'], body=json.loads(JsonPost)).execute() -
Publich
其實就只是把 Draft 的狀態改成 Publish。
# Publish the new post. service.posts().publish(blogId=BLOGID, postId=newpost['id']).execute() -
Insert 解決一切
可以直接用 insert 就好,上面的程式碼只是模擬。
service.posts().insert(blogId=BLOGID, body=body).execute()
參考資料 :
1.Google API Library
2021年4月13日 星期二
Google API - Python 學習筆記
Google API - python ver.
紀錄一些使用 python 呼叫 Google API 的心得
基本前置作業
-
Google Account
首先你要有 google 帳號。
-
Google Cloud Platform project
想要使用 Google Cloud Platform (GCP) 的 service,你需要創建一個 GCP project。左上角 project name 右邊的倒三角按下去新增。
-
Enable API
旁邊的 API&Services => Library => 新增你想要的服務。不過我當初沒有用這步,應該是我用 Oauth 而非 API Keys 的關係 ( 完整介紹 )。
-
Credentials
產生金鑰,我是選 Desktop App,然後下載其 json 檔就樣就好。雖然 Google Guide 其實有給很多講解及步驟,但我沒做也能運行。
安裝 google python api library
-
安裝 google api python
首先安裝 google-api-python-client,點進去有安裝方法,這裡用 Linux 做些紀錄。基本上是安裝一個 virtualenv,讓他新增一個獨立且乾淨的 python 執行環境,然後利用這虛擬環境去下載他門的 library。
# 安裝虛擬 python 環境和 google-api-python-client pip install virtualenv virtualenv <your-env> source <your-env>/bin/activate <your-env>/bin/pip install google-api-python-client -
安裝 google_auth_oauthlib
再來安裝 Oauth 2.0。
<your-env>/bin/pip install google_auth_oauthlib
安裝 google python api library
我寫這篇的主因,因為 google python api library 已經不再更新,有些 sample code 已經過時不能使用所以在這裡紀錄一些可用的 sample code。
-
Sample Code - flow
這是 Google Oauth API Scope 我拿 blogger 做測試。完整的程式碼在 sample.py,輸出結果在 output.json。這裡擷取片段,順序還要參考完整的程式碼。
# 首先是連線設置,Flow 這個 class 是用來讀取你的 Credentials 來認證你的程式。 # client_secrets.json = 你下載的 Oauth json file。 # SCOPES = 你宣告要使用哪些功能 ( ex. gmail, blogger... ) from google_auth_oauthlib.flow import InstalledAppFlow SCOPES = ['https://www.googleapis.com/auth/blogger'] flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) # 用 port 0 連線 GCP,這裡會用預設瀏覽器去做認證,結束後會跟你說可以關閉網頁 creds = flow.run_local_server(port=0) -
Sample Code - token
這裡回傳值 creds,是 GCP 給的暫時性 token,若你的 token 還在且未過期,你就可以用此 token 直接連線而不用再透過瀏覽器認證。
# 如果你有儲存 token,可以直接用 token 重新連線 # token expired 的話且沒過太就的話可以用 creds.refresh(Request()) 做 refresh token creds = Credentials.from_authorized_user_file('token.json', SCOPES) if not creds.valid: if creds.expired and creds.refresh_token: creds.refresh(Request()) -
Sample Code - build
這裡依照 Blogger API Guide 去做取資料。
# 先依照 scope 取得服務,再依照 api 的格式去取得資料 # posts() 跟 list() 都是依照 Blogger API Guide 去填寫的,最後執行 execute() # 回傳會是 dict,要自己轉 json from googleapiclient.discovery import build service = build('blogger', 'v3', credentials=creds) posts = service.posts().list(blogId=BLOGID,maxResults=3).execute()
參考資料 :
1.我跟Google官網
Popular Posts
-
lvalue 、rvalue 基本概念 左值 (lvalue) : 一個佔據某個特定記憶體的值。 右值 (rvalue) : 一個 expression 結束後就消失的值。 基本上這兩個定義包含了全部的值,非左即右,非右即左。 int var = 4; // v...
-
Don't lie, I know you've been thinking it 別說謊了,我知道你一直很渴望 And two times, you let it slip from your lips 兩次了,你那不想讓人聽見的低語 You...
-
You try to hold me down so I became a soldier 你想要控制所以我成為了戰士 Built up all theses walls and now I'm climbing over 監牢般的城牆如今我已越過 Thos...
-
Ooh, don't we look good together? 我們看起來是不是很棒? There's a reason why they watch all night long 這就是為何他們整晚都看著我們的原因 Yeah, kn...
-
重新踏入網頁開發 重新踏入網頁開發 (1) - Nodejs 重新踏入網頁開發 (2) - ES6 重新踏入網頁開發 (3) - Route 重新踏入網頁開發 (4) - Dependency injection ...
-
Job Company : Houzz Job : Back-End Software Engineer Source : Recruiter on LinkedIn Result : 止步二面 Summary 1. 英文程度不佳 :...
-
MD5 Intro MD5 訊息摘要演算法(英語:MD5 Message-Digest Algorithm),一種被廣泛使用的密碼雜湊函式,可以產生出一個128位元(16個字元(BYTES))的雜湊值(hash value),用於確保資訊傳輸完整一致。( ...
-
報告在製作時,一定會遇到要字串轉數字 在 BCB 裡有方便的函式 常見的有 String str = "123" int x = StrToInt(str); float y = StrToFloat(str); 但有時候...
-
BCB TDateTime 時間操作及應用 在 BCB 裡有關時間的操作幾乎都是與 TDateTime 這個 Class 相關 若找不到相關資料可以用 BCB 的 HELP ( 游標移到要查詢的函式或Class 並按 F1 ) 介紹幾個個人有在用的函式 TDate...
-
總覽 讀書心得 - C++ Primer (5th Edition) - Chapter 1 讀書心得 - C++ Primer (5th Edition) - Chapter 2 (1) - Primitive Types 讀書心得 ...