解決Google App Engine HTTPS 認證問題

UPDATE(2008-10-21): 目前Google App Engine 已經支持HTTPS.

 

最近玩兒Google App Engine, 遇到一個很大的問題.那就是Google App Engine的Fetch API, 不支持HTTPS認證.

Google App Engine 文檔 寫道
Note: URL fetching cannot authenticate the server of an https request because there is no certificate trust chain. The proxy accepts all certificates, including self-signed certificates.

 但是很多第三方的API需要HTTPS認證.比如Delicious的API. 沒辦法只好研究了一下認證原理,原來只需要在請求的Header里加入認證語句就可以,而Fetch API可以設置請求Header. 也就是按照如下的方式就可以解決:

import base64
import logging
from google.appengine.api import urlfetch

def login(uname, pwd):
    
    #構建請求認證頭信息
    encoded = base64.b64encode(uname + ':' + pwd)
    authstr = "Basic "+encoded
    autoheaders = {'Authorization':authstr,}

    #需要認證的地址		
    url = "https://....." 
    
    #請求
    result = urlfetch.fetch(url,headers = mheaders)	
		
    logging.info(result.content)

 但這只不過是權宜之計而已, 不知google什麼時候會解決這個問題, 或者根本就不想解決.

 

但是不管怎樣, Google 提供的免費資源, 不用就太浪費了!

 

 

發佈了3 篇原創文章 · 獲贊 0 · 訪問量 2060
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章