令牌C#的Google Auth Exchange代码#

时间:2016-10-18 18:53:06

标签: c# google-oauth

人 当我发出这个http请求时,我收到错误

  

远程服务器返回错误:(400)错误请求

        try
        {

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

            string postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code", authCode, clientid, secret, redirectURI);
            var data = Encoding.ASCII.GetBytes(postData);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            MessageBox.Show(responseString);



        }

        catch (WebException ex)
        {
            MessageBox.Show(ex.Message);
            return null;
        }
    }

我尝试了很多类似的东西:其他链接请求,UrlEncode,将响应放在using()中,但仍然是这个错误的请求。 关键是从响应中获取accces令牌

1 个答案:

答案 0 :(得分:0)

我的重定向URI为null,需要为" urn:ietf:wg:oauth:2.0:oob"。 提示:不要成为2HP。其余