Google API python凭据OAUTH服务器 - 服务器

问题描述 投票:0回答:1

在我的项目中,我开发了一个用于检查的python脚本,并在我的谷歌日历上自动创建约会。首先,我从谷歌开发者控制台OAUTH json文件创建,然后验证并运行我的脚本:

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 lambda函数上传脚本和所有依赖项时服务器响应:

{“errorMessage”:“模块初始化错误”}

START RequestId:2244eefe-e3dd-11e7-a0ce-93ed40a4fa13版本:$ LATEST模块初始化错误:[Errno 30]只读文件系统:'/ home / sbx_user1060'

我认为这是因为身份验证方法在本地计算机上存储和检索json凭据文件,但是如何从服务器 - 服务器而不是客户端 - 服务器验证和运行代码?

提前致谢

python json oauth aws-lambda google-api-python-client
1个回答
1
投票

在代码的凭据部分中,将其更改为从计算机获取默认凭据,如下所示:

#import GoogleCredentials
from oauth2client.client import GoogleCredentials

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

在lambda配置中,将GOOGLE_APPLICATION_CREDENTIALS设置为环境变量键,将凭据json文件名设置为值。

它适用于我使用google api的所有lambda。

© www.soinside.com 2019 - 2024. All rights reserved.