mailchimp-api
mailchimp-api copied to clipboard
Mailchimp Oauth
I don't see any method to use Maillchimp Oauth. (Doc here) If you want I can make a PR that will add two static methods. (Why static? Because the user doesn't need its Api key to use those methods)
- getAuthUrl($client_id, $redirect_uri ) : That returns the url where you redirect the users to make them authenticate with Mailchimp
- getAccessToken($code, $client_id, $client_secret, $redirect_uri ) :That returns the access token of the authenticated user.
This sounds interesting. Could you give an example of how the user would use these methods in their code?
If you want people to login with Mailchimp. You can create button or hyperlink with getAuthUrl
as href.
$client_id = '12345676543';
$redirect_url = 'https://www.some-domain.com/callback_file.php';
$url = Mailchimp::getAuthUrl($client_id, $redirect_url);
echo '<a href="' . $url . '">Login with Mailchimp</a>';
When the user click on the link, he will be asked to login with a Mailchimp account and share his data with the app.
When he clicks on login, he is redirected to the $redirect_url
that you set along with a code in the $_GET
parameter. Ex: https://www.some-domain.com/callback_file.php?code=123456
Then the server use the code received to ask for the user's access_token and redirect the user to a dashboard or wherever you want the user to be.
$code = $_GET['code'];
$client_id = '12345676543';
$client_secret = 'xxxxxxxxxxxx',
$redirect_uri = 'https://www.some-domain.com/callback_file.php';
$access_token = getAccessToken($code, $client_id, $client_secret, $redirect_uri);
Then the server can use the $access_token
of the user to make api calls in his behalf.
This method is the one I used for a project. After getting the $access_token
I used your library to make the API calls. It worked very well.
Support for OAuth would definitely be a good addition to the wrapper!
@aubruz Do you already have the implementation ready? If so, you would do me a big favour by sending them (to me or as a PR here), as I need to use MailChimp with OAuth in a project. Thanks!
@jhogervorst Yes. I created a PR: #242 I hope this will help you.
@aubruz Thanks so much! I’ll use that :)
This is definitely still needed as MailChimp requires partner integrations to use OAuth vs. tokens if you want to be able to list in their marketplace. There are two things that need to be addressed:
-
For me it's less about the OAuth flow that needs to be added, and more about how the keys work in their OAuth setup. Instead of getting a key that looks like this
980as2d257ee0ahj0086a992158379092-us9
you're initially only given980as2d257ee0ahj0086a992158379092
(no datacenter). You then have to make a call to/oauth2/metadata
which will then provide the datacenter you can append to the key. In the current implementation, you can't instantiate the MailChimp class without a key that includes a datacenter. -
The authorization header on the API calls now need to be
Authorization: OAuth 980as2d257ee0ahj0086a992158379092-us9
One thing to note is all keys (those created manually by a user through the Mailchimp UI and those generate through the OAuth flow) will all work with the new authorization header format so it would be a backwards compatible change.
I'm happy to code this up, but I would like some advice on how to approach the first point before writing a lot of code. Thoughts?
Thanks!
Hi @chadhutchins ,
I am working with MailChimp OAuth and I generated the access token. I made a call to the Oauth2/metadata
and received the datacenter as well.
Now, when I want to make api calls like https://us10.api.mailchimp.com/3.0/lists
, I need to pass the OAuth access token and it always tells as the invalid API KEY. I am even specifying the datacenter with they key.
Any help regarding this would be appreciated. Here is the response of the request.
{
"type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title": "API Key Invalid",
"status": 401,
"detail": "Your request did not include an API key.",
"instance": "84de8203-7a59-4629-8ff8-8fcb2c3ff5c2"
}
Thank you :)
EDIT: ---------------------------------------
I figured it out. For anyone using it next time. Use the basic Auth and Provide the access_token as the API Key. Thank you.
Hi @chadhutchins ,
I am working with MailChimp OAuth and I generated the access token. I made a call to the
Oauth2/metadata
and received the datacenter as well.Now, when I want to make api calls like
https://us10.api.mailchimp.com/3.0/lists
, I need to pass the OAuth access token and it always tells as the invalid API KEY. I am even specifying the datacenter with they key. Any help regarding this would be appreciated. Here is the response of the request.{ "type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/", "title": "API Key Invalid", "status": 401, "detail": "Your request did not include an API key.", "instance": "84de8203-7a59-4629-8ff8-8fcb2c3ff5c2" }
Thank you :)
EDIT: ---------------------------------------
I figured it out. For anyone using it next time. Use the basic Auth and Provide the access_token as the API Key. Thank you.
This appears to work. Thanks. The documentation here is incorrect: https://github.com/mailchimp/mailchimp-marketing-node?tab=readme-ov-file#oauth2