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

标签 c# .net oauth-2.0 google-apps gmail-api

我在 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/

相关文章:

oauth-2.0 - KeyCloak用户角色: reset password endpoint for own account only

c# - ABAddressBook 已弃用,如何使用 ABAddressBook.Create 方法?

c# - 用于字母和空格的 .NET RegEx

c# - JsonConvert.DeserializeObject<>(字符串)返回 $id 属性的空值

.net - 有没有办法确定客户端是否收到 .NET 中的 HTTP 响应?

.net - 使用 EF Code First 方法时的 MVC .Net 级联删除

python - Google OAuth2 - 如何更改 expires_in 或 token_expiry 值?

Laravel 护照 : How do i get Access token from Bearer Token

c# - Math.Pow() 与 Math.Exp() C# .Net

javascript - 如何从轮播中获取id并显示特定图像的信息?