2017-12-18 411 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表达式。