Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How do I get the phone number & address from a Google account in Identity Library

I am using the ASP.NET Core 6 Identity library. I added in logging in using my Google account and that works fine. I now want to add getting the account's phone number and address. This is failing.

In program.cs I set:

var auth = builder.Services.AddAuthentication();
var googleClientId = configMgr.GetValue<string>("Authentication:Google:ClientId");
var googleClientSecret = configMgr.GetValue<string>("Authentication:Google:ClientSecret");

// Add authentication services
if ((!string.IsNullOrEmpty(googleClientId)) && !string.IsNullOrEmpty(googleClientSecret))
{
    auth.AddGoogle(options =>
        {
            options.ClientId = googleClientId;
            options.ClientSecret = googleClientSecret;
             options.Scope.Add("https://www.googleapis.com/auth/user.phonenumbers.read");
             options.Scope.Add("https://www.googleapis.com/auth/user.addresses.read");
             options.ClaimActions.MapJsonKey(ClaimTypes.MobilePhone, "phoneNumber");
             options.ClaimActions.MapJsonKey(ClaimTypes.StreetAddress, "address");
        });
}

When I go to log in, Google does tell me that it wants my phone number and address and lets me decide if I want to pass them. I click the check box for both and submit the form.

Then in ExternalLogin.cshtml.cs, in the method

public async Task<IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null, [FromQuery] string following = null)
{
   var info = await _signInManager.GetExternalLoginInfoAsync();
   var phoneNumber = info.Principal.FindFirstValue(ClaimTypes.MobilePhone);
   var address = info.Principal.FindFirstValue(ClaimTypes.StreetAddress);
   // ...
}

Both phoneNumber and address are null. I looked at all claims and all it has is the nameidentifier, name, surname, givenname, & emailaddress.

My Google account at https://myaccount.google.com/personal-info does have a phone & address.

What am I missing?

1 1 147
1 REPLY 1

Hi @DavidThielen,

Welcome to the Google Cloud community!

You are using the scopes "https://www.googleapis.com/auth/user.phonenumbers.read" and "https://www.googleapis.com/auth/user.addresses.read". However, Google does not provide these specific scopes for getting phone numbers and addresses through the OAuth authentication flow. Instead, for phone numbers, Google uses the https://www.googleapis.com/auth/contacts.readonly scope, and for address information, you will need to use the Google People API.

The info.Principal you get from the GetExternalLoginInfoAsync() method only contains basic user information like name, email, and profile. To get phone numbers and addresses, you will need to manually call the Google People API after successful authentication to retrieve those details. Make sure that the Google People API is enabled in your Google Cloud Console.

For more in-depth analysis, you can contact Google Cloud Support. When contacting them, please provide comprehensive details and include screenshots. This will help them better understand and address your issue.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.