mailchimp oauth2 in ASP.NET keep returning invalid_grant

dizete

New Member
I've have reached my frustration-limit on this one, so bare with me on this question:)I am developing a new app at work, that offers integration with MailChimp. Basically it enables users to easily export their customer contact info directly to a MailChimp account (that is, to a specific mailing-list inside MailChimp).All that works, and are somewhat irrelevant to my question.For not asking the user to enter MailChimp-credentials every time, I'm about to implement the oauth2 authorization workflow as described here: http://apidocs.mailchimp.com/oauth2/It works just fine in step 1-4, but step 5 is killing me..Its my first time working with oauth, but I seem to understand the basics.Here my problem:When I do the POST call to the https://login.mailchimp.com/oauth2/token -URI, to get the final access-token, I keep getting the error in JSON result: "invalid_grant"I have checked the request and response streams, that my url is compiled correctly.Here is my code in the controller:(GrantEcoAccess is just to grant access to another app - the rest should be self-explaining)public class HomeController : ApplicationController{ private readonly string authorize_uri = "https://login.mailchimp.com/oauth2/authorize"; private readonly string access_token_uri = "https://login.mailchimp.com/oauth2/token"; private readonly string mailchimp_clientid2 = "xxx"; private readonly string mailchimp_secret2 = "xxx ... public ActionResult GrantEcoAccess() { //if exist: use saved token var user = (Mailchimp_users)Session["user"]; if (!string.IsNullOrWhiteSpace(user.EcoToken)) return RedirectToAction("GrantMailChimpAccess"); // if ! var url = "https://secure.e-conomic.com/secure/api1/requestaccess.aspx?role=superuser&appId=MailChimp&redirectUrl=http://localhost:18017/Home/IncomingToken"; Redirect(url).ExecuteResult(ControllerContext); return null; } public ActionResult IncomingToken(string token) { var user = (Mailchimp_users)Session["user"]; user.EcoToken = token; EcoSession.DataSession.Refresh(System.Data.Objects.RefreshMode.ClientWins, user); EcoSession.DataSession.SaveChanges(); return RedirectToAction("GrantMailChimpAccess"); } public ActionResult GrantMailChimpAccess() { //if exist: use saved token var user = (Mailchimp_users)Session["user"]; if (!string.IsNullOrWhiteSpace(user.MailChimpToken)) return RedirectToAction("Index", "Subscribe"); //if ! var url = string.Format("{0}?response_type=code&client_id={1}&redirect_uri=", authorize_uri, mailchimp_clientid2, "http://127.0.0.1:18017/Home/IncomingMailChimpToken"); Redirect(url).ExecuteResult(ControllerContext); return null; } public ActionResult IncomingMailChimpToken(string code) { var url = "https://login.mailchimp.com/oauth2/token?grant_type=authorization_code&client_id=654486802080&client_secret=bad72d4517bc0de6ecb69d1daee71353&code=" + code + "&redirect_uri=http://127.0.0.1:18017/Home/AuthComplete"; //var url = string.Format("?grant_type=authorization_code&client_id={0}&client_secret={1}&code={2}&redirect_uri={3}", mailchimp_clientid, mailchimp_secret, code, Url.Action("AuthComplete")); Response.Clear(); StringBuilder sb = new StringBuilder(); sb.Append("<html>"); sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>"); sb.AppendFormat("<form name='form' action='{0}' method='post'>", access_token_uri); sb.Append("<input type='hidden' name='grant_type' value='http://stackoverflow.com/questions/12724581/authorization_code'>"); sb.AppendFormat("<input type='hidden' name='client_id' value='http://stackoverflow.com/questions/12724581/{0}'>", mailchimp_clientid2); sb.AppendFormat("<input type='hidden' name='client_secret' value='http://stackoverflow.com/questions/12724581/{0}'>", mailchimp_secret2); sb.AppendFormat("<input type='hidden' name='code' value='http://stackoverflow.com/questions/12724581/{0}'>", code); sb.AppendFormat("<input type='hidden' name='redirect_uri' value='http://stackoverflow.com/questions/12724581/{0}'>", "http://127.0.0.1:18017/Home/AuthComplete"); // Other params go here sb.Append("</form>"); sb.Append("</body>"); sb.Append("</html>"); Response.Write(sb.ToString()); Response.End(); return null; } public ActionResult AuthComplete(string access_token, string expires_in, string scope) { if (string.IsNullOrWhiteSpace(access_token)) throw new Exception("Could not authorize user with MailChimp"); var user = (Mailchimp_users)Session["user"]; user.MailChimpToken = access_token; EcoSession.DataSession.Refresh(System.Data.Objects.RefreshMode.ClientWins, user); EcoSession.DataSession.SaveChanges(); return RedirectToAction("Index", "Subscribe"); }}Can somebody please help me with this? :)Thank you in advance!!
 
Back
Top