Google OAuth不要求返回其他范围

问题描述

我正在对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);

但是,我似乎比所要求的要多。我在返回的范围中得到这个:

openid https://www.googleapis.com/auth/user.gender.read https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/user.birthday.read

我尝试使用隐身模式和不同的浏览器,并且它们都返回相同的内容(认为这可能是缓存问题)。

有人能帮助我吗?

谢谢。

解决方法

使应用程序能够使用增量授权来请求访问上下文中的其他范围。如果将此参数的值设置为true并批准了授权请求,则新的访问令牌还将覆盖用户先前授予应用程序访问权限的所有范围。有关示例,请参见增量授权部分。

摘录自Google文档:https://developers.google.com/identity/protocols/oauth2/web-server

基本上是指用户先前已授予您其他范围。可能是通过登录屏幕还是您使用了相同的clientId