Mailchimp API v3 Python调用报错:list()仅接受1个参数却传入2个
I’ve dealt with this exact pitfall using the Mailchimp Python SDK—let’s walk through what’s causing the error and how to fix it.
The Root Cause
The client.campaigns.list() method doesn’t accept a single positional argument (like a dictionary or list). Instead, it expects keyword arguments that map directly to the API’s query parameters. Your error occurs because you’re passing a dictionary/list as a positional parameter, but the method is designed to take individual keyword params.
Correct Code Example
Here’s the proper way to filter campaigns by list_id:
import mailchimp_marketing as MailchimpMarketing from mailchimp_marketing.api_client import ApiClientError try: client = MailchimpMarketing.Client() client.set_config({ "api_key": "YOUR_API_KEY", "server": "YOUR_SERVER_PREFIX" # e.g., "us1" from your API key }) # Pass list_id as a direct keyword argument response = client.campaigns.list(list_id="YOUR_TARGET_LIST_ID") print(response) except ApiClientError as error: print("Error: {}".format(error.text))
Why Your Previous Attempts Failed
Let’s break down why each of your code snippets threw the same error:
client.campaigns.list({"campaigns": [{"recipients": {"list_id": "xyz"}}]}): Thecampaignsparameter doesn’t exist for the list endpoint—this nested structure isn’t recognized by the API.client.campaigns.list([{"recipients": {"list_id": "xyz"}}]): Passing a list as a positional argument is invalid; the method doesn’t accept list inputs here.client.campaigns.list({"recipients": {"list_id": "xyz"}}): The API doesn’t expect a nestedrecipientsobject for filtering. Thelist_idis a top-level query parameter, not part of a nested structure.client.campaigns.list({"list_id": "xyz"}): You’re passing a dictionary as a single positional argument, but the method expects keyword arguments (e.g.,list_id="xyz"), not a single dict.
Advanced Filtering (If Needed)
If you need to combine multiple filters (e.g., list_id + campaign status), use multiple keyword arguments directly:
# Example: Filter by list_id AND "sent" status response = client.campaigns.list( list_id="YOUR_LIST_ID", status="sent" )
内容的提问来源于stack exchange,提问作者Xlsx

