我正在构建一个MVC应用程序,用户必须使用Google登录才能提供对其信息的离线访问。 我通过JavaScript从View获取Access代码并将其发送到控制器。 从控制器我向Google Api发出POST请求,但我一直收到错误请求400。
有没有人设法成功实现这个?
string baseUri = "https://www.googleapis.com/oauth2/v3/token/";
string parCode = string.Format("code={0}",Code);
string parSecret = "client_secret=******";
string parId = "client_id=*******************";
string parRedirect = "redirect_uri=https://localhost:44301";
string postData = string.Format("{0}&{1}&{2}&{3}", parCode, parRedirect, parId, parSecret);
Uri postUri = new Uri(baseUri);
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] data = encoding.GetBytes (postData);
var request = (HttpWebRequest)WebRequest.Create(baseUri);
request.Method = "POST";
request.ContentType = " application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
HttpWebResponse response;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);