Got (400 Bad Request) while POSTing to get access token

22,775 views
Skip to first unread message

Chem

unread,
Aug 5, 2011, 11:57:07 AM8/5/11
to oauth...@googlegroups.com
The remote server returned an error: (400) Bad Request. at the yellow highlight line.
I think the code i used is correct but don't know why it shows this error
Thx,



Call to Google



Redirect URI

        public ActionResult GReturn()
        {
           
            string token = Request.QueryString["code"];
            string googleURL = "https://accounts.google.com/o/oauth2/token";
           
 string postData = "code=" + token + "&client_id=yyy.apps.googleusercontent.com&client_secret=zzz&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%2Fhome%2Fgreturn";
            
OAuth2 oauth = new OAuth2(googleURL,postData);
            string text = oauth.GetResponseText();
            return View();
}




    public class OAuth2
    {
        private string url;
        private string postData;
        public string outputText;

        public OAuth2() { }

        public OAuth2(string url, string postData)
        {
            this.url = url;
            this.postData = postData;
        }

        public string GetResponseText()
        {

            byte[] buffer = Encoding.ASCII.GetBytes(postData);
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);

            WebReq.Method = "POST";
            WebReq.Host = "accounts.google.com";
            WebReq.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
            WebReq.ContentType = "application/x-www-form-urlencoded";
            WebReq.ContentLength = buffer.Length;
            Stream PostData = WebReq.GetRequestStream();

            PostData.Write(buffer, 0, buffer.Length);
            PostData.Close();

            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);

            outputText = _Answer.ReadToEnd();
            return outputText;

Roshan Dawrani

unread,
Aug 5, 2011, 12:29:10 PM8/5/11
to oauth...@googlegroups.com
On Fri, Aug 5, 2011 at 9:27 PM, Chem <chem...@gmail.com> wrote:

            byte[] buffer = Encoding.ASCII.GetBytes(postData);

Formation of the request to get the access token looks ok to me, so here is a noob guess:

Should you be using Encoding.UTF8.getBytes(postData) here?

--
Roshan
Blog: http://roshandawrani.wordpress.com/
Twitter: @roshandawrani
Skype: roshandawrani

Chem

unread,
Aug 5, 2011, 12:42:09 PM8/5/11
to oauth...@googlegroups.com
it didn't work for me :(

vbtwo

unread,
Sep 4, 2011, 11:23:10 AM9/4/11
to oauth2-dev
I'm having the same problem. Was this ever resolved?

Chem

unread,
Sep 5, 2011, 3:55:57 AM9/5/11
to oauth...@googlegroups.com
no solution for me yet..
it would be nice if someone could help

Marius Scurtescu

unread,
Sep 6, 2011, 1:48:23 PM9/6/11
to oauth...@googlegroups.com
Hi Chem,

What is the response that comes back with 400? Normally there should
be a JSON encoded error response. Do you see that?

Do you have access to a Linux or OS X machine to try the failing POST
using curl? The curl command is:

curl -d "grant_type=authorization_code&client_id=yyy.apps.googleusercontent.com&client_secret=zzz&redirect_uri=http%3A%2F%2Flocalhost%2Fhome%2Fgreturn&code=<token>"
https://sandbox.google.com/o/oauth2/token

(make sure you replace <token> with the actual authorization code and
use real client id and secret)

Marius

vbtwo

unread,
Sep 6, 2011, 2:40:36 PM9/6/11
to oauth2-dev
I fixed my problem.
I was sending a parameter of redirect_url instead of redirect_uri.

After I changed that to send the correct parameter, everything started
working.

Chem, make sure you're sending exact parameters as in the
documentation (and URL encode the values, since that might also cause
issues)

bigwolf

unread,
Sep 13, 2012, 11:58:31 AM9/13/12
to oauth...@googlegroups.com
You may use tools like Advanced Rest Client in Chrome to simulate this request. Google API would return the error in JSON format.

I have encountered this problem and found the the client secret I specified is incorrect.

Just an F.Y.I.

=bigwolf=

Chem於 2011年8月5日星期五UTC+8下午11時57分07秒寫道:

Arun Antony

unread,
Mar 4, 2013, 8:02:12 AM3/4/13
to oauth...@googlegroups.com
i have same problem . Pls share the code if any body cleared this issue 

Tim Bray

unread,
Mar 4, 2013, 4:20:58 PM3/4/13
to oauth...@googlegroups.com

Looking at your request URL, some parts of it have been escaped (I.e. %2F) and others haven't. You'll need to get that straightened out first.

--
You received this message because you are subscribed to the Google Groups "Developer Forum for Google API Access using OAuth2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to oauth2-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages