简体   繁体   English

Python 具有 API 网关的授权方

[英]Python Authorizer with API Gateway

I am trying to make a custom python authorizer with payload format 2.0, right now I'm keeping it really simple and just returning the json "{isAuthorized:true}" regardless of what token is presented.我正在尝试使用有效负载格式 2.0 创建一个自定义 python 授权方,现在我保持它非常简单并且只返回 json“{isAuthorized:true}”,而不管呈现什么令牌。

However, I am still getting failures in cloudwatch saying that the format is incorrect..但是,我仍然在 cloudwatch 中遇到失败,说格式不正确..

I've tried "isAuthorized" as a simple response as well.我也尝试将“isAuthorized”作为简单的响应。

I am using the Simple response mode.我正在使用简单响应模式。

Here is the simple python authorizer:这是简单的 python 授权方:

import os
import re
import json
import logging
import base64
import boto3

def lambda_handler(event, context):
    try:
        response = "{isAuthorized:True}"
        y = json.dumps(response)
        return y;
    except:
        return "";

I've also tried it without the json.dumps like this:我也试过没有 json.dumps 这样的:

...
try: 
    response = {"isAuthorized": True}
    return response;
...

Here's the error in CloudWatch:这是 CloudWatch 中的错误:

The response from the Lambda Authorizer function doesn't match the format that API Gateway expects. Simple response did not include 'isAuthorized'

Any idea what I'm doing wrong?知道我做错了什么吗?

You are returning it as a string, which is not even a valid JSON.您将其作为字符串返回,它甚至不是有效的 JSON。

You can try with:您可以尝试:

        response = {"isAuthorized":True}
        y = json.dumps(response)

or或者

        y = {"isAuthorized":True}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM