【发布时间】:2020-12-12 00:50:48
【问题描述】:
我正在使用 Google 的 oauth 进行测试,并尝试不同的范围。
但是,我随后将我的范围请求缩减为:“https://www.googleapis.com/auth/userinfo.email”
以下是dotnetcore中的更多内容
Dictionary<string, string> queries = new Dictionary<string, string>();
queries.Add("scope", "https://www.googleapis.com/auth/userinfo.email");
queries.Add("access_type", "offline");
queries.Add("include_granted_scopes" ,"true");
queries.Add("response_type", "code");
queries.Add("state", "state");
queries.Add("redirect_uri", "http://localhost:5000/api/authenticate/googauth");
queries.Add("client_id", _clientId);
queries.Add("prompt", "consent");
UriBuilder builder = new UriBuilder();
builder.Host = "accounts.google.com";
builder.Scheme = "https";
builder.Path = "o/oauth2/v2/auth";
//builder.Query = ""
foreach (var query in queries)
{
if (string.IsNullOrEmpty(builder.Query))
{
builder.Query += $"{query.Key}={query.Value}";
}
else
{
builder.Query += $"&{query.Key}={query.Value}";
}
}
var redirectUri = builder.Uri.ToString();
return Redirect(redirectUri);
然后我从返回的代码中检索了访问令牌等。
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("code", code);
values.Add("client_id", _clientId);
values.Add("client_secret",_clientSecret);
values.Add("redirect_uri", "http://localhost:5000/api/authenticate/googauth");
values.Add("grant_type", "authorization_code");
var client = new HttpClient();
var result = await client.PostAsync("https://oauth2.googleapis.com/token", new FormUrlEncodedContent(values));
var content = await result.Content.ReadAsStringAsync();
var convertedContent = JsonSerializer.Deserialize<GoogleAccesstoken>(content);
但是,我得到的似乎比我所要求的要多。我在返回的范围内得到了这个:
openidhttps://www.googleapis.com/auth/user.gender.readhttps://www.googleapis.com/auth/userinfo.profilehttps://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/user.birthday.read
我尝试过使用隐身模式,不同的浏览器都返回相同的内容(认为这可能是缓存问题)。
有人能帮我解决这个问题吗?
谢谢。
【问题讨论】:
标签: oauth google-oauth scopes