Search code examples
pythonboto3aws-secrets-manager

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.


Solution

  • You need to update the section with your required parameters.

    response = client.list_secrets(
              MaxResults=100,
              NextToken='string',  #can be removed if you're secrets are not more than the max result otherwise you needed a while loop 
              Filters=[
                  {
                      'Key': 'name', # update with your filter details 
                       'Values': [
                      'string',
                      ]
                  },
              ],
              SortOrder='desc')