3

I'm trying to get the access token using the below code in C# language, but I'm getting 400 bad request exception.

Code:

WebRequest httpWReq = WebRequest.Create("https://www.box.com/api/oauth2/token");


string postData = "grant_type=authorization_code"; 
postData += "&code=" + Code; 
postData += "&client_id=MY_CLIENT_ID"; 
postData += "&client_secret=MY_CLIENT_SECRET"; 
postData += "&redirect_uri=https://www.google.com";

byte[] data = Encoding.UTF8.GetBytes(postData); 
httpWReq.Method = "POST"; 
httpWReq.ContentType = "application/x-www-form-urlencoding"; 
httpWReq.ContentLength = data.Length; 

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

var response = httpWReq.GetResponse();
var responseStream = response.GetResponseStream();
using (var reader = new StreamReader(responseStream))
{
    var responseReader = reader.ReadToEnd();
    MessageBox.Show(responseReader);
}

But I'm always getting the following Error:

{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}

How to overcome this problem?

Any Help will be appreciated. Thanks in Advance.

Thanks, Harish Reddy

Harish Reddy
  • 275
  • 1
  • 2
  • 7

2 Answers2

1

I see two possible problems, both with this line:

postData += "&redirect_uri=https://www.google.com";
  1. I think you'll need to urlencode the redirect URI.
  2. I presume you don't own the google.com domain, so that's an invalid value. :) You'll need to point back to the domain from which you're making the request. Or better yet, preset this redirect URI on your Box app's configuration page.

Incidentally, you may be interested in checking out the Box API v2 SDK for .Net (and corresponding MVC-based OAuth example) that are up on GitHub and NuGet. (Full disclosure: I contribute to both.)

John Hoerr
  • 7,955
  • 2
  • 30
  • 40
  • Hi John, I tried by encoding the URL as you suggested, but still I'm facing the same problem. And [Box API v2 SDK for .Net](https://github.com/jhoerr/box-csharp-sdk-v2) can be useful after getting the accessToken and RefreshToken. Without these two we cannot able to do anything in that. – Harish Reddy Mar 25 '13 at 05:13
  • for the second reason, I changed the redirect URI to the Local application both in configuration page of Box App and the code, but still I'm facing the same problem. – Harish Reddy Mar 25 '13 at 05:43
  • Harish, the SDK for .Net can (or at least should) provide a complete solution for generating and OAuth tokens. Check out the [MVC-based OAuth example](https://github.com/jhoerr/box-csharp-sdk-v2.sample.oauth) for a demonstration. – John Hoerr Mar 25 '13 at 13:29
0
HttpWebRequest httpWReq =
                (HttpWebRequest)WebRequest.Create("https://api.box.com/oauth2/token");

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "grant_type=authorization_code";
            postData += "&code=" + authorizationCode;
            postData += "&client_id=" + ClientId;
            postData += "&client_secret=" + ClientSecretId;
            byte[] data = encoding.GetBytes(postData);

            httpWReq.Method = "POST";
            httpWReq.ContentType = "application/x-www-form-urlencoded";
            //httpWReq.ContentType = "application/x-www-form-urlencoded";
            httpWReq.ContentLength = data.Length;

            using (Stream stream = httpWReq.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Deep Jadia
  • 111
  • 1
  • 9
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read why [it isn't wise to post code-only answers](https://meta.stackexchange.com/a/148274/341145) – Sᴀᴍ Onᴇᴌᴀ Sep 05 '17 at 07:16