'AWS secrets manager / Got error of pagination token is no longer valid (boto3/python)

I am getting the following error when I try to get all secrets manager list :

botocore.errorfactory.InvalidNextTokenException: An error occurred (InvalidNextTokenException) when calling the ListSecrets operation: The pagination token is no longer valid.

the code is:

    aws_access_key_id="VALID_ACCESS_KEY",
    aws_secret_access_key="VALID_SECRETS_ACCESS",
    region_name="eu-west-1"
    )
    client = session.client('secretsmanager')

    response = client.list_secrets(
        MaxResults=100,
        NextToken='string',
        Filters=[
            {
                'Key': 'name',
                'Values': [
                'string',
                ]
            },
        ],
        SortOrder='desc')
    page_iterator = client.paginate(key='name', PaginationConfig={'MaxItems': 100,'PageSize':100,'StartingToken':'string'})    
    for page in page_iterator:
        contents = page['Contents']
        print ('Received number of results: ', len(contents))
        for obj in contents:
            print (obj['Key'])

I will be happy if anyone can help me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source