在GetResponse()与google交换access_token以获得用户同意时,我收到此错误
远程服务器返回错误:(400)错误请求
我的代码是:
var postData = "code=" + strCode;
postData += "&" + "client_id=" + ConfigurationManager.AppSettings["ClientId"].ToString();
postData += "&" + "client_secret=" + ConfigurationManager.AppSettings["ClientSecretKey"].ToString();
postData += "&" + "redirect_uri=" + ConfigurationManager.AppSettings["RedirectUrl"].ToString(); /*http://my-domain.com/GoogleOAuth/AccessToken*/
postData += "&" + "grant_type=" + "authorization_code";
var data = Encoding.ASCII.GetBytes(postData);
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/oauth2/v4/token");
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = data.Length;
httpWebRequest.Headers["Access-Control-Allow-Origin"] = "http://my-domain.com";
using (var stream = httpWebRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var httpResponse = httpWebRequest.GetResponse();
ViewBag.Result = Json(httpResponse);
var responseFromAPI = "";
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseFromAPI = streamReader.ReadToEnd();
}
我尝试检查我的请求,请求bin,根据https://developers.google.com/identity/protocols/OpenIDConnect#server-flow看起来对我来说是正确的 这是我的请求的屏幕截图
如果有人能告诉我哪里出错了,那将会有很大的帮助。
有没有办法验证我的授权代码,因为出于某种原因,我觉得我的授权代码可能存在问题。
如果需要更多细节,请告诉我。
感谢。
答案 0 :(得分:0)
我无法看到你如何将帖子值应用到正文
https://accounts.google.com/o/oauth2/token
code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code
它们必须采用上述格式的一个长html请求字符串。您的内容类型看起来是正确的。