gpt4 book ai didi

c# - 在 MVC 中从 Google OAuth API 检索出生日期

转载 作者:太空狗 更新时间:2023-10-29 23:50:53 32 4
gpt4 key购买 nike

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

#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 循环中进行调试我得到以下信息

{"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"}

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

或者有什么办法可以解决这个问题请贴出解决方案

最佳答案

我认为您需要将范围设置为 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

关于c# - 在 MVC 中从 Google OAuth API 检索出生日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27859550/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com