Google API C#SDK获取个人资料电子邮件

问题描述

我们正在将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个人资料电子邮件

预先感谢, 叶夫根尼。

解决方法

Profile scope将使您能够访问google people api,但是我认为您需要user.emails.read才能访问其电子邮件地址。

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

如果您只想要用户的电子邮件地址,则使用gmail范围似乎有点过头了。一般来说,您使用的是哪种api,其中一些可以使用户收到电子邮件。