【问题标题】:c# - People API does not return email addressc# - People API 不返回电子邮件地址
【发布时间】:2019-03-28 12:59:21
【问题描述】:

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

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();

【问题讨论】:

  • 您是否在对用户进行身份验证后更改了范围?如果是这样将“我”更改为其他内容,它将强制用户再次登录。
  • 我很害怕,但我不明白当你说“把“我”换成别的东西时,我应该写什么来代替我?
  • "me", filedatastore-demystified

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


【解决方案1】:

您似乎更改了范围。为了查看电子邮件地址,用户必须授予您对电子邮件范围的访问权限。登录用户的凭据信息存储在 %appdata% 中,名称为 me,这是您在下面的代码中给它的名称。

    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% 中的文件

【讨论】:

    猜你喜欢
    • 2018-12-11
    • 2015-08-23
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多