I'm trying to complete authentication with Mailchimp's OAuth implementation, and my access token request is getting an "invalid_grant" error.
Using the ruby HTTParty gem, I'm POSTing data to Mailchimp:
url = 'https://login.mailchimp.com/oauth2/token'
result = HTTParty.post(url,
body: {
grant_type: 'authorization_code',
client_id: ENV["MAILCHIMP_ID"],
client_secret: ENV["MAILCHIMP_SECRET"],
code: auth_hash.credentials.token,
redirect_uri: 'https://requestb.in/1jrbjmi1',
},
headers: {
'Accept': 'application/json'
})
Where:
Has anyone encountered this sort of error? Any idea how to fix it?
I had a similar issue today, my conclusions are as follows:
Unfortunately MailChimp (and many other companies offering OAuth2 endpoints) doesn't provide additional error information - invalid_grant
is shown for a number of different scenarios.
In my case the issue was caused by a slight difference in redirect_uri
- I didn't include the exact match in the token request. It's strict and query parameters must be included and must match, too.
Including a wrong authorisation code unfortunately results in the same error as point above.
My issue with invalid_grant
was caused by using encodeURIComponent
on the redirect URI, and then passing this encoded value to the configuration of the request object. This essentially caused double encoding, which caused the redirect URI's to mismatch and that resulted in this error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With