当前位置: 首页 > 知识库问答 >
问题:

使用OAuth2.0访问Google日历时出错。和服务号:“无效的模拟prn电子邮件地址。”

竺绍辉
2023-03-14

我试图使用Google Calendar API访问使用OAuth2.0和服务帐户的组织日历中不同用户的日历,但我得到了一个错误

“无效的\u请求”“无效的模拟prn电子邮件地址。”。

在Google控制台中,我创建了一个项目,创建了一个服务帐户,启用了“全域委派”,并赋予了“项目所有者”角色,然后获得了一个P12密钥
-安全中

using System;
using System.Windows.Forms;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Services;
using System.Security.Cryptography.X509Certificates;

namespace Google_Calendar
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      string GoogleCertificate = @"testcalendar-209521-772939e76cae.p12"; // keyfile filename 
      string GoogleEmail = @"myserviceaccount@testcalendar-209521.iam.gserviceaccount.com"; // serviceaccount mail
      string GoogleUser = "MyServiceAccount"; // serviceaccount name
      string[] Scopes = new string[] { "https://www.googleapis.com/auth/calendar.readonly" };

      X509Certificate2 certificate = new X509Certificate2(GoogleCertificate, "notasecret", X509KeyStorageFlags.Exportable);
      ServiceAccountCredential credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(GoogleEmail)
        {
          Scopes = Scopes,
          User = GoogleUser
        }.FromCertificate(certificate));
      CalendarService service = new CalendarService(new     BaseClientService.Initializer() { HttpClientInitializer = credential,     ApplicationName = "testcalendar" });

      string CalenderID = "mathias@mydomain.com";
      var CalRequest = service.Events.List(CalenderID);
      CalRequest.TimeMin = DateTime.Now.AddMonths(-1); //optional parameter
      CalRequest.TimeMax = DateTime.Now.AddMonths(+1); //optional parameter
      do
      {
        var events = CalRequest.Execute();  // here I get the error
        foreach (var item in events.Items)
        {
          // do stuff
        }
        CalRequest.PageToken = events.NextPageToken;
      } while (CalRequest.PageToken != null);
    }
  }
}

你知道问题出在哪里吗?我想问题出在我在谷歌某处的设置上。我会错过一步吗?

共有1个答案

哈翔
2023-03-14

在Google支持的帮助下,我解决了这个问题。

1:我使用服务号user
string GoogleUser="MyServiceUser";
我应该使用模拟用户
string GoogleUser="MyAdminUser";

2:当我在管理控制台上添加范围时,我使用服务帐户电子邮件添加了它,然后将其直观地翻译成我项目的ClientID,一切似乎都很好。但事实并非如此。当我转而使用ClientID时,一切都正常。

 类似资料:
  • 我在谷歌云平台上创建了一个项目,并为我的gmail帐户创建了一个OAuth服务帐户id。使用此服务帐户id,我无法阅读电子邮件,因为它说我无法访问此范围Gmail只读。 我通过谷歌搜索发现,我需要从Gsuite admin获得范围访问权限,但如果提供了访问权限,它表示可以使用该权限检索所有组织用户的电子邮件。这个信息正确吗?如果是的话,有没有其他方法可以访问我账户的电子邮件? 下面是我用来访问电子

  • 我有一个具有域范围委托设置的服务帐户,我正在尝试使用该服务帐户创建新帐户(google api服务管理目录),然后向新创建的帐户添加一些预设日历(google api services日历)。 我在目录api方面没有任何问题。我必须使用服务帐户创建一个委派(管理)用户,所有目录api调用都工作正常。 然而,我在日历api调用工作时遇到了问题。 Java依赖性: Java代码: 堆栈跟踪 : 有趣的

  • 我试图使用Oauth 2.0 Google Credential和java中的服务帐户来编辑用户签名,但我收到了一个错误: 我创建了一段代码,在这种模式下将GoogleCredential对象与服务帐户电子邮件一起使用: 我已经在服务帐户的域Google Apps for Business面板中插入了作用域,并在控制台项目中启用了Admin sdk API。 为什么我收到未经授权的邮件?我被允许使

  • 我有一个使用commons电子邮件的项目(http://search.maven.org/#artifactdetails|组织。阿帕奇。commons | commons电子邮件| 1.2 | jar)通过maven发送。我想使用电子邮件模拟类(http://commons.apache.org/email/testapidocs/org/apache/commons/mail/mocks/Mo

  • 我需要在日历上创建谷歌日历活动,并将其他用户添加为该活动的参与者。其目的是向应用程序用户发送日历事件,而无需征得他们的同意(O-Auth)。 在阅读了谷歌的留档后,我发现我需要一个服务号。所以我从我们的G-Suite的一个电子邮件地址创建了一个项目和一个服务号,noreply@xxxxx.com并为其启用了日历应用编程接口。 我创建并下载了一个密钥对(JSON),其内容是, 根据文档,我开始编写身

  • 我们正在尝试使用EWS管理的应用编程接口集成办公365。我们使用办公365管理中心创建了一个具有模拟角色的服务号。现在,我们如何使用该服务号访问应用程序中其他用户的数据(如邮件、联系人、日历)?谢谢,罗希特