Bad request for http request google drive offline access

I'm trying to get offline access with the refreshtoken. With a httprequest I have to get a new accessToken with the saved refreshToken.

But I keep getting a Bad Request 400 with no further explanation what's wrong. Can someone tell me what's wrong with this code or post a code that does work??

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");

authenticator.ApplyAuthenticationToRequest(request);

request.Method = "POST";

String para = "code='"+refreshtoken+"'&client_id='" + ClientCredentials.CLIENT_ID + "'&client_secret='" + ClientCredentials.CLIENT_SECRET + "'&grant_type='refresh_token'";

System.IO.Stream requestStream = request.GetRequestStream();

System.IO.StreamWriter streamwriter = new System.IO.StreamWriter(requestStream);

streamwriter.Write(para);
streamwriter.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();

StreamReader reader = new StreamReader(stream);
String result = reader.ReadToEnd();

Answers


You should use the client library for .NET, and if you could paste the complete request and response, that might help.


Public Function httpReq(uri As String, para As String, host As String, method As String) As String
    Dim responseData As String = ""
    Dim postData As String = para
    Dim encoding As New ASCIIEncoding()
    Dim byte1() As Byte = encoding.GetBytes(postData)

    Dim httpWebRequest As System.Net.HttpWebRequest = Net.WebRequest.Create(uri)
    httpWebRequest.Method = method
    httpWebRequest.ContentType = "application/x-www-form-urlencoded"
    httpWebRequest.ContentLength = byte1.Length

    Dim newStream As IO.Stream = httpWebRequest.GetRequestStream()
    newStream.Write(byte1, 0, byte1.Length)
    newStream.Close()

    Dim httpWebResponse As System.Net.HttpWebResponse = httpWebRequest.GetResponse()
    If httpWebResponse.StatusCode = System.Net.HttpStatusCode.OK Then
        Dim responseReader As System.IO.StreamReader = New System.IO.StreamReader(httpWebResponse.GetResponseStream())
        responseData = responseReader.ReadToEnd()
    End If
    httpWebResponse.Close()
    Return responseData
End Function

Need Your Help

How to make jQuery slide open and close with CSS class

jquery css slidetoggle

How can I my CSS styling background and text to slide up into the jQuery action smoothly? Right now upon clicking again to close, the background color will disappear and then the text will slide u...

Absolutely deathly wxWidgets Pain

c++ visual-studio-2010 wxwidgets

I've never used wxWidgets before, and I'd love to try it out, but I've run into a million problems getting started.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.