mailchimp oauth2 in ASP.NET keep returning invalid_grant

和自甴很熟 提交于 2019-12-02 02:07:15

Step 4 is "Your application must make an out-of-band request to the access_token_uri using the code"

The main point here is "out of band". You have to build and send a post request server-side. The client should not have your mailchimp_secret

Your IncomingMailChimpToken could look like this :

    public ActionResult IncomingMailChimpToken(string code)
    {
        string mcPostData = String.Format(
            "grant_type={0}&client_id={1}&client_secret={2}&code={3}&redirect_url={4}",
            System.Web.HttpUtility.UrlEncode("authorization_code"),
            System.Web.HttpUtility.UrlEncode(mailchimp_clientid2),
            System.Web.HttpUtility.UrlEncode(mailchimp_secret2),
            System.Web.HttpUtility.UrlEncode(code),
            System.Web.HttpUtility.UrlEncode("http://127.0.0.1:18017/Home/AuthComplete")
            );
        WebRequest request = WebRequest.Create(access_token_uri);
        // Set the Method property of the request to POST.
        request.Method = "POST";
        request.ContentType = "application/json";
        byte[] byteArray = Encoding.UTF8.GetBytes(mcPostData);
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Cleanup the streams and the response.
        reader.Close ();
        dataStream.Close ();
        response.Close ();

        // parse the json responseFromServer to extract token, expires_in and scope
        // and call AuthComplete with these params
    }

You should send your request parameters in the body using post, if you were using curl php you would do this:

$value = http_build_query($params); //params is an array
curl_setopt($ch, CURLOPT_POSTFIELDS, $value);

Value should look like this:

grant_type=authorization_code&client_id=635959587059&client_secret=0da3e7744949e1406b7b250051ee1a95&code=1edf2589e664fd317f6a7ff5f97b42f7&redirect_uri=http%3A%2F%2F192.168.1.8%2Foauth%2Fcomplete.php

Notice that you should create a body request in the form a query string, don't send json, they wont't find your params. If you get an invalid grant response or something after doing this, check that the redirect uri you used to get the first code is EXACTLY the same as the one you are sending to get the token.

Also, to the ones using PHP, to match what the mailchimp documentation states use this:

curl_setopt($ch, CURLOPT_USERAGENT, 'oauth2-draft-v10');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

If you don't want to implement it yourself, there is this nice wrapper, which handles oAuth2 AND RESTapi-calls to MailChimp.

https://github.com/jamierytlewski/eepOAuth2-MVC

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!