首页 > 解决方案 > 如何在 python3.6 中的 AWS Lambda 中存储和访问 microsoft office365 帐户令牌

问题描述

标签: python-3.xaws-lambdaoffice365apibearer-tokenaws-lambda-layers

解决方案


我遇到了同样的问题。lambda 文件系统是临时的,因此每次运行该函数时都需要执行 auenticate 过程,并且 o365 库将要求提供 url。因此,请尝试将您的令牌 (o365_token.txt) 保存在 S3 中,而不是在 lambda 文件系统中获取它并使用此令牌进行身份验证。我希望这段代码可以帮助你:

import boto3
bucket_name = 'bucket_name' 

# replace with your bucket name
filename_token = 'o365_token.txt' 

# replace with your AWS credentials
s3 = boto3.resource('s3',aws_access_key_id='xxxx', aws_secret_access_key='xxxx') 

# Read the token in S3 and save to /tmp directory in Lambda 
s3.Bucket(bucket_name).download_file(filename_token, f'/tmp/{filename_token}') 

# Read the token in /tmp directory
token_backend = FileSystemTokenBackend(token_path='/tmp', 
token_filename=filename_token)

# Your azure credentials
credentials = ('xxxx', 'xxxx')
account = Account(credentials,token_backend=token_backend)

# Then do the normal authentication process and include the refresh token command
if not account.is_authenticated:
    account.authenticate()
account.connection.refresh_token()

推荐阅读