Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to set up offline access for a Google Analytics API application using OAuth2 and I'm using ASP.NET to POST my authorization code in exchange for a refresh token but I can't get a response from the server to give me the refresh token.

I've used the example code from the MSDN documentation for a post request so I can only assume this is correct, however I receive the error message "The remote server returned an error: (400) Bad Request at System.Net.HttpWebRequest.GetResponse()":

using System;
using System.IO;
using System.Net;
using System.Text;

WebRequest request = WebRequest.Create ("https://accounts.google.com/o/oauth2/token?code=xxxmyauthorizationcodexxx&client_id=xxxxxxxxx.apps.googleusercontent.com&client_secret=xxxxxxxxxxxx&redirect_uri=https://mysite.com/oauth2callback&grant_type=authorization_code");
request.Method = "POST";
string postData = "code=xxxmyauthorizationcodexxx&client_id=xxxxxxxxx.apps.googleusercontent.com&client_secret=xxxxxxxxxxxx&redirect_uri=https://mysite.com/oauth2callback&grant_type=authorization_code";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();

WebResponse response = request.GetResponse ();
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();

reader.Close ();
dataStream.Close ();
response.Close ();

I have successfully used the GET method to retrieve the authorization code to begin with and I'm following this documentation: https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse

I'm also using a https website to make the requests and I'm manually refreshing my authorization code as it expires. Does anyone have a solution for this problem?

EDIT: For anyone experiencing the same issue, firstly look at aeijdenberg's response below, but my solution was that the authorization code I used for the code parameters expires pretty instantaneously - I kept refreshing my page without request a new one. To get the data just display the content from the responseFromServer variable.

share|improve this question
Thanks this helped me :) – user1926138 Jul 24 at 12:03

1 Answer

up vote 3 down vote accepted

It looks like you are passing the parameters twice. Once in the query string:

WebRequest request = WebRequest.Create ("https://accounts.google.com/o/oauth2/token?code=xxxx...

And then again as POST data. I would suggest removing the query string, e.g. POST directly to "https://accounts.google.com/o/oauth2/token".

Also suggesting ensuring that all parameters are URL encoded if you're not already doing so: http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx

share|improve this answer
thanks you for this, this helped me get what I needed! – mmmoustache Jun 13 at 9:18
Thanks a ton for the solution. – user1926138 Jul 24 at 12:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.