【问题标题】:Retrieving date of birth from Google OAuth API in MVC从 MVC 中的 Google OAuth API 检索出生日期
【发布时间】:2015-01-09 11:26:18
【问题描述】:

我添加了以下范围来访问用户的数据

#region Google
 var googleAuthenticationOptions = new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = ConfigurationManager.AppSettings["Google_AppID"],
            ClientSecret = ConfigurationManager.AppSettings["Google_AppSecret"]
        };
        googleAuthenticationOptions.Scope.Add("profile");
        googleAuthenticationOptions.Scope.Add("email");
googleAuthenticationOptions.Provider = new GoogleOAuth2AuthenticationProvider()
        {
            OnAuthenticated = async context =>
            {
                //context.Identity.AddClaim(new Claim("birthday", context.User.GetValue("birthday").ToString()));
                string claimType;
                bool bAddClaim = false;
                foreach (var claim in context.User)
                {

                    claimType = string.Empty;
                    bAddClaim = false;
                    switch (claim.Key)
                    {
                        case "given_name":
                            claimType = "FirstName";
                            bAddClaim = true;
                            break;
                        case "family_name":
                            claimType = "LastName";
                            bAddClaim = true;
                            break;
                        case "gender":
                            claimType = "gender";
                            bAddClaim = true;
                            break;
                    }

                    if (bAddClaim)
                    {
                        string claimValue = claim.Value.ToString();
                        if (!context.Identity.HasClaim(claimType, claimValue))
                            context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Google"));
                    }
                }
            }
        };

context.user 通过放置断点在侧 foreach 循环中进行调试 我正在获取以下信息

{ “子”:“101111724115925537597”, “名称”:“阿洛克纳特”, "given_name": "好吧", "family_name": "内斯", “个人资料”:“https://plus.google.com/101111724115925537597”, “图片”:“https://lh3.googleusercontent.com/XdUIqdMkCWA/AAAAAAAAAAAI/AAAAAAAAAA/4252rscbv5M/ 照片.jpg", “电子邮件”:“aloknathbabuji786@gmail.com”, “email_verified”:是的, “性别”:“男性”, “语言环境”:“en” }

因此,即使添加了“个人资料”范围,我也不会将生日作为返回值的一部分。我已确保该个人资料的生日可见性设置为公开,即对所有人可见。为了从 Google 检索生日,我需要执行任何特定范围或特定设置吗?

或者有什么办法可以解决这个问题,请发布解决方案

【问题讨论】:

  • 您是否尝试将 nuget 包 Google.Apis.OAuth2.v2 客户端库 添加到您的项目中?这也为您的范围提供了一个枚举,因此您可以说:Scope = { Oauth2Service.Scope.UserinfoProfile, Oauth2Service.Scope.UserinfoEmail } 而不是使用字符串

标签: c# asp.net-mvc oauth google-oauth owin


【解决方案1】:

我认为您需要将范围设置为 https://www.googleapis.com/auth/plus.login 才能获得此 stackoverflow 用户建议的出生日期:

Retrieving date of birth and marital status with Google OAuth API

更多信息也可以在这里找到: https://developers.google.com/+/api/oauth#plus.login

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-05
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多