C#-People API不返回电子邮件地址

时间:2019-03-28 12:59:21

标签: c# google-api google-api-dotnet-client google-people-api

我正在尝试获取登录用户的电子邮件地址和名称。我可以看到用户名,但电子邮件地址显示为空。这是我编写的用于检索这两个值的代码:

using Google.Apis.Auth.OAuth2;
using Google.Apis.People.v1;
using System.Net;
using System.Threading;
using Google.Apis.Services;
using Google.Apis.People.v1.Data;


UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
 new ClientSecrets
               {
                   ClientId = "jfms2vr52dm9eghchabasa5.apps.googleusercontent.com",
                   ClientSecret = "xxxxxxxxxxxx"
               },
               new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/userinfo.email" },
               "me",
               CancellationToken.None).Result;

        // Create the service.
        var service = new PeopleService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "M_Test1",
        });

        PeopleResource.GetRequest peopleRequest = service.People.Get("people/me");          
        peopleRequest.RequestMaskIncludeField = "person.names,person.emailAddresses,person.birthdays";
        Person profile = peopleRequest.Execute();

enter image description here

1 个答案:

答案 0 :(得分:0)

您似乎已更改了范围。为了查看电子邮件地址,用户必须已授予您访问电子邮件范围的权限。登录用户的证书存放在%appdata%中,名称为,这是您在下面的代码中为其指定的名称。

    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
 new ClientSecrets
               {
                   ClientId = "jfms2vr52dm9eghchabasa5.apps.googleusercontent.com",
                   ClientSecret = "xxxxxxxxxxxx"
               },
               new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/userinfo.email" },
               "me",  // change this to reset login
               CancellationToken.None).Result;

这只是表示用户的字符串。您可以将其更改为其他任何内容,它将提示您再次登录。或者,您也可以删除%appdata%

中的文件