简体   繁体   中英

Error when trying to exchange authorization code for access code using Google API

I am using the following C# code to try to get exchange my authorization code for an access code:

        const string TokenExchangeEndPointUrl = "https://accounts.google.com/o/oauth2/token";
        string data = string.Format(
              "code={0}&client_id={1}&client_secret={2}&redirect_url={3}&grant_type=authorization_code",
              (value), (_clientId), (_clientSecret), (RedirectUri));

        var utfenc = new UTF8Encoding();
        byte[] buffer = utfenc.GetBytes(data);
        var req = (HttpWebRequest) WebRequest.Create(TokenExchangeEndPointUrl);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = buffer.Length;

        using (Stream strm = req.GetRequestStream())
        {
            strm.Write(buffer, 0, buffer.Length);
            strm.Close();
        }
        var response = (HttpWebResponse) req.GetResponse();

The very last line is where the error is occurring. The error I'm getting is (400) Bad Request. It occurs on the last line. From the examples I've seen online, I appear to be doing it right. I've run the code through Fiddler2 and it appears to match what I'm getting from Google's OAuth Playground , which makes this problem really weird. I've triple checked my ClientId and Client Secret. They are correct for my application. Can someone please point me in the right direction?

I actually figured it out. I was putting "redirect_url". It should be "redirect_uri"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM