简体   繁体   中英

connecting to web api using c#

I'm trying to autheniicate using the code below to silverpop although I'm getting a status code of 400 when attempting. Any suggestions as I'm not sure what else to try?! I can see the call going out using Fiddler but I've ran out of ideas. Many thanks

The server is returning the following error message:

Code snippet

        var httpWReq = (HttpWebRequest)WebRequest.Create("https://api5.silverpop.com/oauth/token");
        var postData = string.Format("&grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);

                    // Also try this string but I get the same response
        //var postData = string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);

        var encoding = new ASCIIEncoding();
        var data = encoding.GetBytes(postData);
        httpWReq.Method = "POST";
        httpWReq.ContentType = "x-www-form-urlencoded";
        httpWReq.ContentLength = data.Length;

        using (var stream = httpWReq.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)httpWReq.GetResponse();

        var result = new StreamReader(response.GetResponseStream()).ReadToEnd();

Response from server

The remote server returned an error: (400) Bad Request.

This seems to work:

        var httpWReq = (HttpWebRequest)WebRequest.Create("https://api5.silverpop.com/oauth/token" + string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", clientId, clientSecret, refreshToken));
        var postData = "";

        var encoding = new ASCIIEncoding();
        var data = encoding.GetBytes(postData);
        httpWReq.Method = "POST";
        httpWReq.ContentType = "x-www-form-urlencoded";
        httpWReq.ContentLength = data.Length;

        using (var stream = httpWReq.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)httpWReq.GetResponse();

        var result = new StreamReader(response.GetResponseStream()).ReadToEnd();

try change change from

var postData = string.Format("&grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);

to

var postData = string.Format("?grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}", config.ClientId, config.ClientSecret, config.RefreshToken);

This usually indicates that you are contacting the server in the wrong way. Are you perhaps missing a '?' in your post data?

EDIT

Try:

var client = new HttpClient();
var content = new FormUrlEncodedContent(<put content here>);
var response = await client.PostAsync("<your API URL>", content);

If you check the request with Fidler you'll see the parameters in body in both cases (with preceding & and with preceding ? signs).

With & sign: 与&标志

With ? sign: 与?标志

The proper thing to do would be to remove the ? or the & signs in front and then the parameter name in body will be grant_type. Code that works:

var postData = string.Format("grant_type=refresh_token&client_id={0}&client_secret={1}&refresh_token={2}",
            config.ClientId, config.ClientSecret, config.RefreshToken);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM