调用日历 API 时出错:主体必须是电子邮件地址



我们在 1 年多前编写了一个软件来同步来自 Google 日历的事件。它自 2018 年 3 月以来一直在工作,但本月它突然停止在我们用于我们和客户的每个 Gsuite 实例上工作。

问题是我们正在使用服务帐户通过谷歌日历API v3访问日历资源事件。 使用服务帐户凭据进行身份验证后,对于每个服务调用,我们现在都会遇到错误:

"错误:\"invalid_request\", 描述:"主体必须是电子邮件地址", Uri:\"\">

我无法在任何地方找到有关此问题的答案,这听起来像是Google服务的政策更改,但我无法确定这一点。 似乎Google API正在抱怨服务帐户电子邮件,这实际上并不是真正有效的电子邮件。它是这样的格式:

mydomain.com_(client_id)313.......@resource.calendar.google.com

您认为问题可能与服务帐户电子邮件地址有关吗? 为什么它在四月之前工作,现在停止工作?

一种尚未尝试的可能解决方案:

我播种可以使用"委派"来计算服务计数。 如果我尝试使用有效的用户电子邮件并将其委派给我的服务帐户,会发生什么情况?

/* function I use to authenticate my service account to Google Calendar
* service. serviceAccount is the email (Principal not working?) and json is 
* the key generated for him.
*/
public void bindCalendarService(string json, string serviceAccount)
{
var cr = Newtonsoft.Json.JsonConvert.DeserializeObject<PersonalServiceAccountCred>(json);
ServiceAccountCredential xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.client_id, cr.token_uri)
{
Scopes = new string[] {
CalendarService.Scope.Calendar,
},
User = serviceAccount
}.FromPrivateKey(cr.private_key));
CalendarService calendarService = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = xCred
});
this.calendarService = calendarService;
}

您需要在Google帐户安全性上授予对不安全应用的访问权限, 试试这个 : https://support.google.com/accounts/answer/6010255?hl=en 如果它不起作用,请与我交谈。

我今天用另一种方式解决了这个问题。

1)我试图像wx解释的那样实现委派: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority 它有效。

2)在没有授权的情况下,我以这种方式更改了我的帖子:

var credentialParameters = NewtonsoftJsonSerializer.Instance.Deserialize(json);

ServiceAccountCredential xCred = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(**credentialParameters.ClientEmail**)
{
Scopes = new string[] {
CalendarService.Scope.Calendar
},
User = serviceAccount,         // service account's email
}.FromPrivateKey(credentialParameters.PrivateKey));

即使以这种方式工作。 为什么上面的代码在 1 年后停止工作仍然是一个困惑。

希望它能帮助某人 谢谢

相关内容

最新更新