AWS API Gateway 可以使用 AWS Identity and Access Management (IAM) 进行方法身份验证。下面是一个使用 AWS_IAM 进行方法身份验证的示例解决方法:
现在,API Gateway 将使用 AWS_IAM 进行方法身份验证。
以下是一个使用 AWS_IAM 进行方法身份验证的代码示例(使用 Python 3):
import boto3
# 创建 API Gateway 客户端
apigateway_client = boto3.client('apigateway')
# 定义 API ID 和资源 ID
api_id = 'your_api_id'
resource_id = 'your_resource_id'
# 定义要添加的方法
http_method = 'GET'
authorization_type = 'AWS_IAM'
# 添加方法身份验证
response = apigateway_client.put_method(
restApiId=api_id,
resourceId=resource_id,
httpMethod=http_method,
authorizationType=authorization_type
)
# 将 API 部署到特定阶段
stage_name = 'your_stage_name'
response = apigateway_client.create_deployment(
restApiId=api_id,
stageName=stage_name
)
# 在 API 部署中添加方法权限
arn = 'your_lambda_function_arn'
function_name = 'your_lambda_function_name'
response = apigateway_client.add_permission(
restApiId=api_id,
resourceArn=arn,
functionNamespace=function_name,
action='lambda:InvokeFunction',
principal='apigateway.amazonaws.com'
)
这是一个基本的示例,可以根据你的具体需求进行修改和扩展。