Node.js中使用Mailchimp API OAuth2时,无法完成授权码换访问令牌
I’ve run into the exact same frustrating issue with Mailchimp’s OAuth2 token exchange step before—when you follow the docs to the letter and it still won’t work! Let’s break down the most common fixes that got me past this roadblock:
Common Issues & Fixes
Fix the
&in your curl command
The official docs sometimes use HTML-encoded&in the data string, but curl expects raw&to separate parameters. Replace all instances of&with just&in your--datapayload—this is a super easy oversight that breaks the request.Double-check your redirect URI (critical!)
This is the #1 culprit for token exchange failures:- The
redirect_uriin your curl request must be fully URL-encoded (use a standard URL encoder if you’re not sure about the correct format). - It must match exactly what you registered in your Mailchimp Developer Account—including trailing slashes, capitalization, and any query parameters. Even a tiny mismatch (like missing a slash) will get your request rejected.
- The
Ensure your authorization code is valid
Authorization codes are single-use and expire quickly (around 10 minutes). If you’ve already tried using that code once, you’ll need to go back through the full authorization flow to grab a fresh one.Verify Client ID/Secret accuracy
It’s shockingly easy to accidentally copy extra spaces or miss a character when grabbing these from the Mailchimp dashboard. Double-check that bothclient_idandclient_secretmatch exactly what’s listed in your app settings—no typos allowed!Add the Content-Type header explicitly
While curl usually sets this automatically, some environments need a nudge. Try adding theContent-Typeheader to your request to ensure the payload is interpreted correctly:curl --request POST \ --url https://login.mailchimp.com/oauth2/token \ --header "Content-Type: application/x-www-form-urlencoded" \ --data "grant_type=authorization_code&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&redirect_uri=YOUR_ENCODED_REDIRECT_URI&code=YOUR_AUTHORIZATION_CODE"Check for network/proxy blockages
If you’re running this from a server in a region with restricted access, make surelogin.mailchimp.comis reachable. Test with a direct curl request without proxies to rule out connectivity issues.
If none of these work, share the exact error response you’re getting (e.g., invalid_grant, redirect_uri_mismatch)—that’ll help narrow down the problem even faster!
内容的提问来源于stack exchange,提问作者Kumar

