【问题标题】:Google API C# SDK get profile emailGoogle API C# SDK 获取个人资料电子邮件
【发布时间】:2021-01-16 10:49:12
【问题描述】:

我们正在将 Google api C# SDK 集成到我们的应用程序中,并遵循 Google“网络应用程序”工作流程。 我们需要检索当前经过身份验证的 Google 用户电子邮件。

我们使用以下方法之一让它工作:

  1. 直接向https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=xxx发起http请求。

  2. 使用 Google people api C# sdk

    var peopleService = new PeopleServiceService(new BaseClientService.Initializer { HttpClientInitializer = result.Credential, ApplicationName = "Test App" });
    
    var peopleRequest = peopleService.People.Get("people/me");
    peopleRequest.RequestMaskIncludeField = new List<string> { "person.EmailAddresses" };

    var profile = peopleRequest.Execute();
  1. 使用 Google Gmail api C# sdk
    var gmailService = new Google.Apis.Gmail.v1.GmailService(new BaseClientService.Initializer                                                                     
        {
            HttpClientInitializer = result.Credential, ApplicationName = "Test App"                                                                    
        });

    var gmailProfile = gmailService.Users.GetProfile("me").Execute();
    var userGmailEmail = gmailProfile.EmailAddress;

我们使用的范围是:

“https://www.googleapis.com/auth/gmail.readonly”“电子邮件”“个人资料”

上述示例运行良好,但我们不想使用 https://www.googleapis.com/auth/gmail.readonly,因为它是“受限”范围。

所以,我们想知道是否有机会使用 C# SDK 中的某种方法调用 Google 个人资料电子邮件?

提前致谢, 叶夫根尼。

【问题讨论】:

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


    【解决方案1】:

    Profile scope 将允许您访问 google people api 但是我认为您需要 user.emails.read 才能访问他们的电子邮件地址。

    https://www.googleapis.com/auth/user.emails.read 查看您的电子邮件地址

    如果您只想要用户的电子邮件地址,那么使用 gmail 范围似乎有点过头了。您通常使用哪种 api,其中一些有获取用户电子邮件的方法。

    【讨论】:

    • 嗨@DalmTo。谢谢,我已经尝试过您建议的范围和 people.get() 方法 - 它也很有效。但是我的问题是 C# SDK 中是否有一些“开箱即用”的方法来获取 UserInfo 对象,例如 SomeBaseGoogleService.GetUserInfo().Execute()。是否存在类似的东西?
    • 客户端库是围绕允许您访问不同的 api 而构建的。在这种情况下,人员 API。因此,没有简单的方法可以为您提供此信息。您需要自己创建一个人员服务请求您需要从该 API 获得的任何数据
    猜你喜欢
    • 2017-09-24
    • 2015-09-22
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 2011-09-28
    相关资源
    最近更新 更多