【发布时间】:2023-03-21 16:36:01
【问题描述】:
此代码显示标签,我不确定它的作用或作用。 代码正在运行。
但现在我想在 listBox1 中列出我的 gmail listBox 中的最后 50 封电子邮件。 我已经创建了一个 client_secrets.json 文件,在该文件中我有我的 gmail 帐户...my@gmail.com 以及客户端密码和客户端 ID。
这是我尝试过的,但出现了一些错误:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using System.Threading;
using System.IO;
using Google.Apis.Util.Store;
namespace Google_Gmail
{
public partial class Form1 : Form
{
static string[] Scopes = { GmailService.Scope.GmailReadonly };
static string ApplicationName = "Youtube Uploader";
public Form1()
{
InitializeComponent();
ListMessages(GmailService,)
}
private GmailService GmailService()
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// 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("me");
return service;
}
public static List<Google.Apis.Gmail.v1.Data.Message> ListMessages(GmailService service, String userId, String query)
{
List<Google.Apis.Gmail.v1.Data.Message> result = new List<Google.Apis.Gmail.v1.Data.Message>();
UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List(userId);
request.Q = query;
do
{
try
{
ListMessagesResponse response = request.Execute();
result.AddRange(response.Messages);
request.PageToken = response.NextPageToken;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
} while (!String.IsNullOrEmpty(request.PageToken));
return result;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
第一个错误就行了:
static string[] Scopes = { GmailService.Scope.GmailReadonly };
在 GmailService 上我收到错误:
错误 1“Google_Gmail.Form1.GmailService()”是一个“方法”,在给定的上下文中无效
第二个这是我在调用方法 ListMessages 作为用户 ID 和查询时应该在构造函数中添加什么? userid 是我在 json 文件中的客户端 ID 吗?应该是什么查询?
【问题讨论】:
-
好的发现我错误地调用了我的方法GmailService将其更改为GmailServices解决了所有错误。现在工作。
标签: c# .net winforms google-api