【问题标题】:Google OAuth Returning Additional Scopes Without RequestingGoogle OAuth 在不请求的情况下返回其他范围
【发布时间】: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


    【解决方案1】:

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

    从谷歌文档中提取:https://developers.google.com/identity/protocols/oauth2/web-server

    基本上意味着用户之前已授予您其他范围。可能是通过登录屏幕或您使用相同 clientId 的地方

    【讨论】:

    • 要添加更多详细信息,问题出在以下行:queries.Add("include_granted_scopes" ,"true"); 告诉 Google 包括以前在当前授权中授予的范围。
    • 抱歉回复太晚了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 2016-04-30
    • 1970-01-01
    相关资源
    最近更新 更多