在MVC中从Google OAuth API获取出生日期

5

我已添加以下范围以访问用户数据

#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"));
                    }
                }
            }
        };

在 foreach 循环中使用断点调试 context.user 时,我获取到以下信息:
{
"sub": "101111724115925537597",
"name": "Alok Nath",
"given_name": "Alok",
"family_name": "Nath",
"profile": "https://plus.google.com/101111724115925537597",
"picture":"https://lh3.googleusercontent.com/XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/
          photo.jpg",
"email": "aloknathbabuji786@gmail.com",
"email_verified": true,
"gender": "male",
"locale": "en"
}
即使添加了“profile”范围,我也没有在返回值中看到生日信息。我已确保将生日可见性设置为公开,即所有人都可以看到。我是否需要特定的范围或特定的设置才能从Google中检索出生日期?
如果有解决此问题的方法,请发布解决方案。

你尝试在项目中添加NuGet包Google.Apis.OAuth2.v2 Client Library了吗?这还为您的范围提供了枚举,因此您不必使用字符串,只需说:Scope = { Oauth2Service.Scope.UserinfoProfile, Oauth2Service.Scope.UserinfoEmail } - Frank Fajardo
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接