{"message":"The security token included in the request is invalid."}

{"message":"The security token included in the request is invalid."}

我正在使用 Serverless Now 作为通过 Slack 调用 AWS 的工具 API。该脚本在本地和服务器上运行良好,但我在使用 Now Platform 使其运行时遇到问题。

这是我遇到的错误:

{"message":"The security token included in the request is invalid."}

有几个关于此的问题,但它们似乎没有回答我的问题,因为我看到 AWS 凭证配置正确 运行 cat ~/.aws/credentials.

我正在使用 AWS Requests Auth:

auth = AWSRequestsAuth(aws_access_key=os.environ['AWS_ACCESS_KEY_ID'],
                                   aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
                                   aws_host='restapiid.execute-api.us-east-1.amazonaws.com',
                                   aws_region='us-east-1',
                                   aws_service='execute-api')

headers = {"Content-Type":"application/json"}
response = requests.post('https://restapiid.execute-api.us-east-1.amazonaws.com/path', auth=auth, headers=headers, data=payload)

由于错误通常是 403,我怀疑是 Signature expired 问题,但我不知道如何将 Now 时间戳与 AWS 同步。

我的缺点:os.environ['AWS_ACCESS_KEY_ID']os.environ['AWS_SECRET_ACCESS_KEY']在Now平台是reserved variables。当您使用 slack API 或简单脚本在本地使用它们时,一切都很好,因为它将使用配置文件,但是现在您需要存储并重命名它们:

now secret add ak "[your aws access key]"
now secret add sk "[your aws secret key]"
now -e AWS_AK=@ak  -e AWS_SK=@sk

这样您就可以在您的代码中使用它们

os.environ['AWS_AK']
os.environ['AWS_SK']