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

Google API python credentials OAUTH server-server

在我的项目中,我开发了一个 python 脚本来检查并在我的 google 日历上自动创建约会。 首先,我从 google 开发控制台 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": "module initialization error" }

START RequestId: 2244eefe-e3dd-11e7-a0ce-93ed40a4fa13 Version: $LATEST module initialization error: [Errno 30] Read-only file system: '/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.

的 lambda