4

Having the following code:

                var request = HttpWebRequest.Create("https://api.channeladvisor.com/oauth2/token");
            request.ContentType = "text/html";               
            request.Method = "POST";
            request.Headers.Add("cache-control","no-cache");
            request.Headers.Add("Authorization", "Basic " + Base64Translator.ToBase64(System.Text.Encoding.ASCII, devKey+":"+sharedSecret));          

            string postData = " grant_type=refresh_token&refresh_token=a12SL1bHhJwerxM2NFth2efZw0yIW7462kAhR43UCJA";

            var data = Encoding.ASCII.GetBytes(postData);
            request.ContentLength = data.Length;

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

            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

What's wrong? I can't do the request but I can using external programs like Postman. Do you see anything wrong in the header?

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

Thanks!

2
  • What is the error ? Commented Aug 21, 2017 at 15:22
  • Try comparing your request with a good request using Fiddler. Commented Aug 26, 2017 at 17:58

1 Answer 1

2

Your content type should be application/x-www-form-urlencoded.

request.ContentType = "application/x-www-form-urlencoded";
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I forgot to add it!
I did it! Error: "The remote server returned an error: (400) Bad Request."
guess: you have a leading space in PostData.
Hehe I tried adding an space (1 of my test... ) but wihtout space still not work :'(

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.