C# 使用Gmail API C使用Imap客户端登录Gmail时出错#
我正在做一个需要使用Gmail API访问Gmail的项目。 但我在尝试登录Imap时不断遇到此错误: {“状态”:“400”,“方案”:“持有人”,“范围”:“} 我在这里缝,有人问这个问题,并说它解决了正确的令牌范围 在我的情况下,它不起作用,错误仍然不断出现C# 使用Gmail API C使用Imap客户端登录Gmail时出错#,c#,api,gmail,gmail-api,gmail-imap,C#,Api,Gmail,Gmail Api,Gmail Imap,我正在做一个需要使用Gmail API访问Gmail的项目。 但我在尝试登录Imap时不断遇到此错误: {“状态”:“400”,“方案”:“持有人”,“范围”:“} 我在这里缝,有人问这个问题,并说它解决了正确的令牌范围 在我的情况下,它不起作用,错误仍然不断出现 static string[] Scopes = { GmailService.Scope.GmailReadonly }; static string ApplicationName = "Gmail API .NE
static string[] Scopes = { GmailService.Scope.GmailReadonly };
static string ApplicationName = "Gmail API .NET Quickstart";
static void Main(string[] args)
{
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/gmail-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "Sapir", 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");
credential.Token.Scope = "https://mail.google.com/";
ImapClient ic = new ImapClient("imap.gmail.com","astechnetivot@gmail.com", credential.Token.AccessToken,AuthMethods.SaslOAuth, 993, true);
ic.SelectMailbox("INBOX");
Console.WriteLine(ic.GetMessageCount());
MailMessage[] mm = ic.GetMessages(0, 1);
foreach (MailMessage m in mm)
{
Console.WriteLine(m.Subject + " " + m.Date.ToString());
}
ic.Dispose();
Console.Read();
}
当您使用过期的访问令牌时,似乎会出现错误消息,请检查您的访问令牌并确保它未过期。这里有一个状态为400的相关问题:当您使用过期的访问令牌时,似乎会出现错误消息,请检查您的访问令牌并确保它未过期。以下是状态为400的相关问题: