Google Calendar API - Bad Request (400) Trying To Swap Code For Access Token

By : JTeagle

Having got an authorisation code from Google for accessing a user's calendar, I'm now trying to swap this for an access token. According to their own docs:

The actual request might look like:

POST /o/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded


My attempt to access this is as follows (C#):

string url = "";
WebRequest request = HttpWebRequest.Create(url); 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded"; 

string body = "code=<the_code_I_received>&\r\n"
    + "client_id=<my_client_id>&\r\n"
    + "client_secret=<my_client_secret>&\r\n"
    + "redirect_uri=http://localhost:4300\r\n"
    + "grant_type=authorization_code&\r\n"
byte[] bodyBytes = Encoding.ASCII.GetBytes(body); 
request.ContentLength = bodyBytes.Length ; 
Stream bodyStream = request.GetRequestStream(); 
bodyStream.Write(bodyBytes, 0, bodyBytes.Length); 


'http://localhost:4300' is exactly the same as I put in the original request (and it was valid because I got the code back through listening as a web server on that port), but I also tried just 'http://localhost' just in case.

I tried a number of suggestions such as setting the proxy to null (no change) and changing Accept (wasn't allowed to add that header to the web request).

In each case I get the HTTP 400 - bad request back (the try / catch fires with an exception stating that).

Putting a trailing slash after /token (I'll try anything!) resulted in a 500 Internal Server Error, so that wasn't it either.

Any idea what I'm doing wrong?

By : JTeagle


Do you need the new lines \r\n in the body? This code works for me...

var req0 = WebRequest.Create("");
req0.Method = "POST";
string postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",
code, //the code i got back
"", "XJxxxFy",
"http://localhost:1599/home/oauth2callback"); //my return URI

byte[] byteArray = Encoding.UTF8.GetBytes(postData);
req0.ContentType = "application/x-www-form-urlencoded";
req0.ContentLength = byteArray.Length;
using (Stream dataStream = req0.GetRequestStream())
    dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse response = req0.GetResponse())
    using (var dataStream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(dataStream))
         string responseFromServer = reader.ReadToEnd();
         var ser = new JavaScriptSerializer();
            accessToken = ser.DeserializeObject(responseFromServer);
catch (WebException wex){ var x = wex; }
catch (Exception ex){var x = ex;}
By : ajd

This video can help you solving your question :)
By: admin