【发布时间】:2019-08-19 02:49:12
【问题描述】:
我正在尝试学习如何实现 Google 的 Oauth2 SSO 登录功能。到目前为止,我已经到了一切正常的地步,除非我尝试使用 C# 中的 REST 向“https://accounts.google.com/o/oauth2/token”发送请求以获取 Access_code/Token。我已经在 google 上注册了我的本地主机和相应的端口,并且我设法让我的 POST 请求在 POSTMAN 中工作,但是当我尝试在 C# 中发送请求时,我的 HttpRequestMessage 返回 400 错误请求。
首先,我有一个 Authorization.aspx 页面,该页面运行一个命令,该命令将用户重定向到用户登录的 google 授权页面,然后重定向到我的页面 http://localhost:64716/GoogleCallBack.aspx 获取响应。在页面中,它获取授权码并尝试向基本 URL 发送 POST 请求。我试过在它工作的 POSTMAN 中运行它(尽管只有一个新的授权码)。所以我知道问题出在我的 C# 代码上。我尝试过使用 webrequest,也尝试过将媒体类型更改为 application/Json 和 application/x-www-form-urlencoded
protected void Page_Load(object sender, EventArgs e)
{
string Error = Request.QueryString["error"];
string Code = Request.QueryString["code"];
if (Error != null) { }
else if (Code != null)
{
string UserId = Request.QueryString["state"];
int Id = Convert.ToInt32(UserId);
string AccessToken = string.Empty;
string RefreshToken = ExchangeAuthorizationCode(Id, Code, out AccessToken);
string Url = "Authorize.aspx?UserId=" + UserId;
Response.Redirect(Url, true);
}
}
private string ExchangeAuthorizationCode(int userId, string code, out string accessToken)
{
string baseurl = "https://accounts.google.com/o/oauth2/token";
accessToken = string.Empty;
string ClientSecret = ConfigurationManager.AppSettings["ClientSecrete"];
string ClientId = ConfigurationManager.AppSettings["ClientId"];
string RedirectUrl = "http://localhost:64716/GoogleCallBack.aspx";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseurl);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/Json"));
var nvc = new List<KeyValuePair<string, string>>();
nvc.Add(new KeyValuePair<string, string>("Code", code));
nvc.Add(new KeyValuePair<string, string>("Client_id", ClientId));
nvc.Add(new KeyValuePair<string, string>("Client_secret", ClientSecret));
nvc.Add(new KeyValuePair<string, string>("Redirect_uri", RedirectUrl));
nvc.Add(new KeyValuePair<string, string>("Grant_type", "authorization_code"));
nvc.Add(new KeyValuePair<string, string>("access_type", "offline"));
var req = new HttpRequestMessage(HttpMethod.Post, client.BaseAddress) { Content = new FormUrlEncodedContent(nvc) };
var resres = client.SendAsync(req).Result;
return "temp";
}
}
我希望我的响应是状态码 200,带有来自 google 的 access_token(就像在 Postman 中一样)
【问题讨论】:
-
我没有看到 oauth 令牌试图像这样发送。你的标题与邮递员的匹配吗?
-
在这个特定的示例中,不,在 Postman 中,我使用 application/x-www-form-urlencoded 而不是 application/json,但我也尝试过添加它。它没有解决我的问题。