gpt4 book ai didi

c# - MVC 外部登录 - 使用 Google People API

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:02 26 4
gpt4 key购买 nike

我有一个 C# MVC 项目。我试图通过从谷歌检索用户信息来帮助用户注册过程。我想访问名字、姓氏、电子邮件和手机号码。都是必填字段。我相信我需要使用 Google People API。 Google+ API 一直运行良好,但没有手机号码。我不确定如何获取这些数据。目前在 startup.auth 我有:

        var googleOptions = new GoogleOAuth2AuthenticationOptions()
{
ClientId = ConfigurationManager.AppSettings["GoogleClientId"],
ClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"],
Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = context =>
{
context.Identity.AddClaim(new Claim("urn:google:accesstoken", context.AccessToken, ClaimValueTypes.String, "Google"));
context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email));
context.Identity.AddClaim(new Claim(ClaimTypes.Uri, context.User["image"]["url"].ToString()));
return Task.FromResult(true);
}
}
};
googleOptions.Scope.Add("https://www.googleapis.com/auth/user.phonenumbers.read");
googleOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
googleOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
app.UseGoogleAuthentication(googleOptions);

在我的 Controller 中我有:

        if (loginInfo.Login.LoginProvider == "Google")
{
if (loginInfo.Email == null)
loginInfo.Email = GetSchemasClaimValue(loginInfo, "emailaddress");
firstName = GetSchemasClaimValue(loginInfo, "givenname");
lastName = GetSchemasClaimValue(loginInfo, "surname");
mobilePhone = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.MobilePhone);


//proPic = GetSchemasClaimValue(loginInfo, "uri");
}

除了手机以外的所有信息都可以访问并按需要工作。我只是不确定如何检索这些数据。我希望它会在 loginInfo 中显示为 Claim,但在这种情况下不存在任何声明。系统提示用户授予应用程序访问手机的权限,所以我对为什么没有声明感到有点困惑。是否需要在我的 startup.auth 中添加声明?那将如何工作?任何帮助将不胜感激。

最佳答案

我在 my sample Xamarin project 中完成了类似的功能,即从 Google People API 获取数据后端托管在 Azure 移动应用程序(又名应用程序服务)上。我是这样做的。

using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization =
AuthenticationHeaderValue.Parse("Bearer " + accessToken);

using (HttpResponseMessage response = await client.GetAsync("https://www.googleapis.com/plus/v1/people/me"))
{
var googlePlusUserInfo =
JsonConvert.DeserializeObject<GooglePlusUserInfo>(await response.Content.ReadAsStringAsync());

googlePlusUserInfo.Email = googlePlusUserInfo.Emails.Count() > 0 ?

googlePlusUserInfo.Emails.First().EmailAddress : "";

googlePlusUserInfo.ProfilePicure.ImageUrl =
googlePlusUserInfo.ProfilePicure.ImageUrl.Split(new char[] { '?' })[0];

return googlePlusUserInfo;
}
}

Complete code on Github .

如果需要检索电话号码,那么我们可以使用:

googlePlusUserInfo.PhoneNumbers.CanonicalForm

当然,the model GooglePlusUserInfo也需要更新以匹配首先返回的 JSON 结构。返回电话号码的 Google 用户配置文件的 JSON 是

{
"resourceName": "people/...",
"etag": "...",
"phoneNumbers": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "1"
}
},
"value": "88880000",
"canonicalForm": "+65888800000",
"type": "mobile",
"formattedType": "Mobile"
}
]
}

希望这对您有所帮助,如果我错了请纠正我。

关于c# - MVC 外部登录 - 使用 Google People API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46671360/

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