mailchimp-api icon indicating copy to clipboard operation
mailchimp-api copied to clipboard

Mailchimp Oauth

Open aubruz opened this issue 7 years ago • 8 comments

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.

aubruz avatar Oct 27 '17 08:10 aubruz

This sounds interesting. Could you give an example of how the user would use these methods in their code?

drewm avatar Nov 17 '17 16:11 drewm

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.

image

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.

aubruz avatar Nov 17 '17 20:11 aubruz

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 avatar May 02 '18 11:05 jhogervorst

@jhogervorst Yes. I created a PR: #242 I hope this will help you.

aubruz avatar May 02 '18 19:05 aubruz

@aubruz Thanks so much! I’ll use that :)

jhogervorst avatar May 02 '18 19:05 jhogervorst

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:

  1. 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 given 980as2d257ee0ahj0086a992158379092 (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.

  2. 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!

chadhutchins avatar Feb 25 '19 15:02 chadhutchins

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.

raghavddps2 avatar Jul 16 '20 07:07 raghavddps2

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

madmacc avatar Feb 19 '24 01:02 madmacc