2

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.
3
  • postData section doesn't seems good!! shouldn't it start with ? instead & ? Also try debuging, the value for post data to check whether it contains special character! Commented Nov 21, 2013 at 17:32
  • httpWReq.ContentType = "application/x-www-form-urlencoded";? Commented Nov 21, 2013 at 17:53
  • What do you suggest to use then? Thanks Commented Nov 22, 2013 at 8:24

4 Answers 4

3

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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);

1 Comment

I wouldn't expect the post data to have anything at the beginning, other than the name of the first value.
0

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);

1 Comment

I've made the changes as suggested by others which is now using the '?' at the beginning of the querystring although I'm still getting the same issue. Any other suggestions please?
0

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

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.