2

我有一个 C# MVC 项目。我试图通过从 Google 检索用户信息来帮助用户注册过程。我想访问名字、姓氏、电子邮件和手机号码。都是必填字段。我相信我需要使用 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);

在我的控制器中,我有:

        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 中显示为声明,但这种情况下不存在声明。提示用户授予应用访问手机的权限,所以我有点困惑为什么没有索赔。是否需要在我的 startup.auth 中添加声明?那将如何运作?任何帮助,将不胜感激。

4

2 回答 2

0

https://www.googleapis.com/auth/user.phonenumbers.read范围仅适用于Google People API。您无法从登录中获取该信息。

您需要在登录 Google People API 后发出请求以获取所需信息。请参阅他们文档中的示例:https ://developers.google.com/people/v1/read-people

于 2017-10-11T23:09:19.777 回答
0

我在我的示例 Xamarin 项目中完成了一个类似的功能,即从 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;
    }
}

Github 上的完整代码

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

googlePlusUserInfo.PhoneNumbers.CanonicalForm

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

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

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

于 2017-10-10T17:34:59.010 回答