gpt4 book ai didi

c# - Google API - Calendar API 正在运行,但 Gmail API 返回 '' unauthorized_client”

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:19 27 4
gpt4 key购买 nike

我在 Google Apps 中有一个项目,我为其启用了日历 API 和 Gmail API(为服务器到服务器身份验证生成了一个“.p12” key )。我已经成功地通过以下方式读取/写入我的 Google 日历帐户:

 private CalendarService GetCalendarService()
{
var certificate = new X509Certificate2(_googleCredentialsFilePath, "notasecret", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
var serviceEmail = "599172797645-dthli52ji7j0j53gacmqigvs694bu7vs@developer.gserviceaccount.com";
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceEmail)
{
Scopes = new[] { CalendarService.Scope.Calendar, CalendarService.Scope.CalendarReadonly }
}.FromCertificate(certificate));

var applicationName = ConfigurationManager.AppSettings["ApplicationName"];
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = applicationName,
});

return service;
}

使用服务对象,我可以像这样在我的日历中创建事件:

var newEvent = new Event()
{
Summary = calendarEvent.Summary,
Description = calendarEvent.Description,
Start = new EventDateTime()
{
DateTime = calendarEvent.StartDateTime,
TimeZone = _ianaTimezone,
},
End = new EventDateTime()
{
DateTime = calendarEvent.EndDateTime,
TimeZone = _ianaTimezone,
}
};

var request = _calendarService.Events.Insert(newEvent, googleCalendarId);
var result = request.Execute(); // CREATE EVENT SUCCESSFULLY

现在,对于 Gmail API 客户端,我想使用服务帐户所有者电子邮件发送电子邮件(与我用来登录 Google Dev Console 的电子邮件相同——“mypersonalemail@gmail.com”

private GmailService GetGmailService()
{
var certificate = new X509Certificate2(_googleCredentialsFilePath, "notasecret", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
var serviceEmail = "599172797645-dthli52ji7j0j53gacmqigvs694bu7vs@developer.gserviceaccount.com";
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceEmail)
{
Scopes = new[] { GmailService.Scope.MailGoogleCom, GmailService.Scope.GmailCompose, GmailService.Scope.GmailModify, GmailService.Scope.GmailSend },
User = "mypersonalemail@gmail.com"
}.FromCertificate(certificate));

var applicationName = ConfigurationManager.AppSettings["ApplicationName"];
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = applicationName,
});

return service;
}

但是当我尝试测试请求草稿列表的客户端时:

ListDraftsResponse draftsResponse = _gmailService.Users.Drafts.List("mypersonalemail@gmail.com").Execute();

然后我得到错误:

"Error:\"unauthorized_client\", Description:\"Unauthorized client or scope in request.\", Uri:\"\"" string

我已经使用 mypersonalemail@gmail.com 登录到 Google Developers Console 并在

API Manager > Permissions > Add service account owner added "mypersonalemail@gmail.com" as the service email owner (probably redundantly)

我是否需要 Google Apps for Work 才能通过服务帐户从我自己的 gmail 帐户发送/阅读电子邮件?我的应用程序目前作为 Web 应用程序托管在 Azure 中。

最佳答案

检查 This

并确保您访问 google.Admin 帐户以引用您创建的应用程序的服务帐户客户端 ID,时区格式如下“美国/凤凰城”。

关于c# - Google API - Calendar API 正在运行,但 Gmail API 返回 '' unauthorized_client”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com