admin管理员组

文章数量:1122832

I'm really new to all this REST API stuff. Recently, I ran into a problem while trying to create the GET method handler (403 Forbidden) and a json message "Missing Authentication Token" being returned. I'm using AWS Gateway, Lambda Functions and DynamoDB. The POST function that is used to create items in DB works fine. It's my first question on stack overflow thus I might have put too many screenshots.

The code:

import json
import boto3
from botocore.exceptions import ClientError
from decimal import Decimal

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
buffer_table = dynamodb.Table('buffer')


def lambda_handler(event, context):
    try:
        print('Request event:', event)

        if event.get('httpMethod') != 'GET':
            return build_response(405, {'Error': 'Method Not Allowed'})

        query_parameters = event.get('queryStringParameters')
        if not query_parameters:
            return build_response(400, {'Error': 'Query parameters are required'})

        if 'message_id' not in query_parameters:
            return build_response(400, {'Error': 'message_id is required'})
        try:
            message_id = int(query_parameters['message_id'])
        except ValueError:
            return build_response(400, {'Error': 'message_id must be a number'})

        if 'user_id' not in query_parameters:
            return build_response(400, {'Error': 'user_id is required'})
        try:
            user_id = int(query_parameters['user_id'])
        except ValueError:
            return build_response(400, {'Error': 'user_id must be a number'})

        return get_message(message_id, user_id)

    except Exception as e:
        print('Error:', e)
        return build_response(500, {'Error': 'Internal Server Error'})


def get_message(message_id, user_id):
    try:
        response = buffer_table.get_item(
            Key={
                'MessageID': message_id,
                'UserID': user_id
            }
        )

        if 'Item' not in response:
            return build_response(404, {
                'Error': f'Message with MessageID {message_id} and UserID {user_id} not found'
            })

        return build_response(200, {
            'Operation': 'GET',
            'Message': 'SUCCESS',
            'Item': response['Item']
        })

    except ClientError as e:
        print('DynamoDB Client Error:', e)
        error_message = e.response.get('Error', {}).get('Message', 'Unknown error')
        return build_response(500, {'Error': error_message})


class DecimalEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Decimal):
            if obj % 1 == 0:
                return int(obj)
            else:
                return float(obj)
        return super(DecimalEncoder, self).default(obj)


def build_response(status_code, body):
    return {
        'statusCode': status_code,
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': json.dumps(body, cls=DecimalEncoder)
    }

I tried to use another code examples on GitHub but I still caught the same error. Also double checked all of the IAM permissions.

本文标签: python403 ForbiddenMissing Authentication Token for GET Method in AWS API GatewayStack Overflow