2

I have the Rest API Url https://www.box.com/api/oauth2/token. To get the Refresh/Access Tokens respectively.

When i try this in PostMan (Chrome Extension) Its working fine. Below i have attached the screen shot.

Getting the Access Token Reference : https://developers.box.com/oauth/

enter image description here

But the same i tried in Code its giving me the Bad Request Error.

    public string PostToUrl(string url, string data)
    {
        string results = String.Empty;
        WebRequest req = WebRequest.Create(url);
        req.Method = WebRequestMethods.Http.Post;
        byte[] byteArray = Encoding.UTF8.GetBytes(data);
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = byteArray.Length;
        Stream dataStream = req.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse res = req.GetResponse();
        dataStream = res.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        results = reader.ReadToEnd();
        return results;
    }

    public override void TestConnection(TimeSpan timeout)
    {
       string json = PostToUrl("https://www.box.com/api/oauth2/token", "code=" + Code + "&grant_type=authorization_code&client_id=" + ClientId + "&client_secret=" + ClientSecret);
    }

here my question is....

How do i need to form the request for this rest API call? How the Multi Form Data's and values need to be merged to that request?

Box Request through Post Man:

enter image description here

Box Request from My Code :

enter image description here

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80

2 Answers2

2

Try to compare both request with fiddler to catch the differences.

You can also simplify your Rest handling code with RestSharp.

sslazio1900
  • 132
  • 1
  • 7
  • Thanks for your response. I have compared already through fiddler. But i'm not getting how to add those content disposition with form data and its values. i have updated the question – RajeshKdev Jun 16 '14 at 09:01
1

I have added the NameValueCollection to the Headers. Like Below

            var nameValue = new NameValueCollection
            {
                {"grant_type", "authorization_code"},
                {"client_id", ClientId},
                {"client_secret", ClientSecret},
                {"code", RefreshToken}
            };

            request.Headers.Add(nameValue);

Initial Reference Link

Now it's working fine.

Community
  • 1
  • 1
RajeshKdev
  • 6,365
  • 6
  • 58
  • 80