【问题标题】:Reading email of other user using Gmail API使用 Gmail API 阅读其他用户的电子邮件
【发布时间】:2020-12-19 11:28:27
【问题描述】:

我正在编写一个 C# 桌面应用程序,我想在其中阅读用户的所有电子邮件(除我的以外)。我已经在 GMail 控制台上创建了一个项目。当我阅读自己的电子邮件时,应用程序运行良好,但我无法阅读其他用户的电子邮件/消息。 GMail 文档中可用的示例代码读取所有者电子邮件 ID 的标签,但能够读取其他用户的标签(使用 GMail 电子邮件 ID/个人资料 ID)

GMail Example

UserCredential credential;

using (var stream = new FileStream("CredentialFile.json", FileMode.Open, FileAccess.Read))
{
    // The file token.json stores the user's access and refresh tokens, and is created
    // automatically when the authorization flow completes for the first time.
    string credPath = "token.json";
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        Scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)).Result;
    Console.WriteLine("Credential file saved to: " + credPath);
}

logMe("Token : " + credential.Token.AccessToken);

// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
});


// Define parameters of request.
UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("XXXXXXXXXXXXXXXXXXXXX");

// List labels.
IList<Label> labels = request.Execute().Labels;
Console.WriteLine("Labels:");
if (labels != null && labels.Count > 0)
{
    foreach (var labelItem in labels)
    {
        Console.WriteLine("{0}", labelItem.Name);
    }
}
else
{
    Console.WriteLine("No labels found.");
}

Console.Read();

【问题讨论】:

  • 您需要使用其他人的凭据登录 gmail,因为出于非常明显的安全问题,您无法阅读其他人的电子邮件
  • 但是根据 Google 文档,我们需要在 Google 控制台上验证我们的应用程序。然后我们就可以读取用户的邮件了。

标签: c# google-api google-oauth gmail-api google-api-dotnet-client


【解决方案1】:

FileDataStore 将您登录的用户的凭据存储在 %appData% 中,用您作为“用户”提供的用户名表示

如果要更改当前登录的用户,则需要将“用户”更改为其他内容。它只是一个字符串,所以它可以是你喜欢的任何东西来表示这个用户。然后让用户使用他们的帐户登录。

如果您想了解 FileDataStore 的工作原理,这可能会让您感兴趣Google .net – FileDatastore demystified

【讨论】:

    猜你喜欢
    • 2018-05-11
    • 2015-06-04
    • 1970-01-01
    • 2019-04-24
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2020-08-22
    相关资源
    最近更新 更多