Google API python credentials OAUTH server-server



在我的项目中,我已经开发了一个python脚本以进行检查,并在我的Google日历上自动创建约会。首先,我是从Google Dev Console 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":"模块初始化错误" }

开始requestID:2244EEFE-E3DDDDDD-11E7-A0CE-93ED40A4FA13版本:$最新 模块初始化错误:[ERRNO 30]只读文件系统:'/home/sbx_user1060'

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

预先感谢

在您的代码的凭据部分中,将其更改以从计算机中获取默认凭据,例如:

#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的所有Lambdas。

最新更新