【问题标题】:Google API python credentials OAUTH server-serverGoogle API python 凭证 OAUTH 服务器-服务器
【发布时间】:2017-12-18 10:54:34
【问题描述】:

在我的项目中,我开发了一个 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】:

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

    #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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2013-10-24
      • 2019-07-23
      • 2014-08-02
      • 2020-01-15
      • 2015-07-24
      • 2016-06-06
      相关资源
      最近更新 更多