2017-12-18 671 views
0

在我的項目中,我開發了一個python腳本進行檢查並自動在我的Google日曆上創建一個約會。 首先,我從谷歌開發者控制檯OAUTH JSON文件創建,然後驗證和運行我的腳本:Google API python證書OAUTH服務器服務器

SCOPES = 'https://www.googleapis.com/auth/calendar' 
CLIENT_SECRET_FILE = 'client_id.json' 
APPLICATION_NAME = 'Google Calendar API Python Quickstart' 


def get_credentials(): 

home_dir = os.path.expanduser('~') 
credential_dir = os.path.join(home_dir, '.credentials') 
if not os.path.exists(credential_dir): 
    os.makedirs(credential_dir) 
credential_path = os.path.join(credential_dir, 
           'calendar-python-quickstart.json') 

store = Storage(credential_path) 
credentials = store.get() 
if not credentials or credentials.invalid: 
    flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) 
    flow.user_agent = APPLICATION_NAME 
    if flags: 
     credentials = tools.run_flow(flow, store, flags) 
    else: # Needed only for compatibility with Python 2.6 
     credentials = tools.run(flow, store) 
    print('Storing credentials to ' + credential_path) 
return credentials 


credentials = get_credentials() 
http = credentials.authorize(httplib2.Http()) 
service = discovery.build('calendar', 'v3', http=http) 

當我從我的本地機器全部完成運行它,但是當我上傳腳本和使用AWS拉姆達所有依賴功能時,我測試了服務器responde:

{ "errorMessage": "module initialization error" }

START RequestId: 2244eefe-e3dd-11e7-a0ce-93ed40a4fa13 Version: $LATEST module initialization error: [Errno 30] Read-only file system: '/home/sbx_user1060'

我想這是因爲身份驗證方法存儲和retrive本地計算機上的JSON證書文件,但我怎麼能驗證和從服務器到服務器運行代碼,而不是客戶端 - 服務器?

在此先感謝

回答

1

在你的代碼的憑證部分,將其更改爲得到機器的默認憑據,是這樣的:

#import GoogleCredentials 
from oauth2client.client import GoogleCredentials 

credentials = GoogleCredentials.get_application_default() 
service = discovery.build('calendar', 'v3', credentials=credentials) 

在您的拉姆達配置集GOOGLE_APPLICATION_CREDENTIALS作爲環境變量的關鍵和你的證書json文件名作爲值。

它適用於我所有使用google api的lambda表達式。