【问题标题】:Error 403 Google API Email Settings .Net getting signature错误 403 Google API 电子邮件设置 .Net 获取签名
【发布时间】:2015-07-06 15:50:56
【问题描述】:

我真的在潜心研究一个让我睡不着觉的问题。

基本上,我们有一个应用程序,它为公司的每个用户获取 GMail('domaincompany.com')上的 HTML 电子邮件签名,并将更新的个人数据设置在新的 HTML 签名上。

这是如何实现的:

public static string getGMailFirma(string mail) {
  GoogleMailSettingsService service = null;
  string[] domainUserGMail;

  string result;

  try {
    /* Recuperamos los datos del usuario de GMail:  *
     *    - Pos 0: usuario                          *
     *    - Pos 1: dominio                          */
    domainUserGMail = Util.getUserDomainGMail(mail);

    /* Iniciamos el servicio de GApps */
    service = new GoogleMailSettingsService(domainUserGMail[1], "company.FirmaCorporativa");
    service.setUserCredentials(PropertiesManager.getUserGApps(), PropertiesManager.getPassGApps());

    result = service.RetrieveSignature(domainUserGMail[0]).getPropertyValueByName("signature").ToString();

    return result;

  } catch (Exception ex) {
    throw ex;
  }
}

因此,几周前这不再起作用,执行后显示错误消息:“执行身份验证请求返回意外结果:404”:

result = service.RetrieveSignature(domainUserGMail[0]).getPropertyValueByName("signature").ToString();

搜索网络文档和不同的资源,我们意识到它应该实现OAuth2,所以我们尝试了这个代码修改:

public static string getGMailFirma(string mail) {
  GoogleMailSettingsService service = null;
  string[] domainUserGMail;

  string result;

  try {
    const string ServiceAccountEmail = "xxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";

    var certificate = new X509Certificate2("Key_admin.p12", "notasecret", X509KeyStorageFlags.Exportable);



    ServiceAccountCredential credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(ServiceAccountEmail) {
          Scopes = new[] { "https://apps-apis.google.com/a/feeds/emailsettings/2.0/" }
        }.FromCertificate(certificate));

    if (!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
      throw new InvalidOperationException("Access token request failed.");

    var requestFactory = new GDataRequestFactory(null);
    requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken);


    /* Recuperamos los datos del usuario de GMail:  *
     *    - Pos 0: usuario                          *
     *    - Pos 1: dominio                          */
    domainUserGMail = Util.getUserDomainGMail(mail);

    /* Iniciamos el servicio de GApps */
    service = new GoogleMailSettingsService(domainUserGMail[1], "CompanyFirmaCorporativa");
    service.RequestFactory = requestFactory;

    service.setUserCredentials(PropertiesManager.getUserGApps(), PropertiesManager.getPassGApps());

    result = service.RetrieveSignature(domainUserGMail[0]).getPropertyValueByName("signature").ToString();

    return result;

  } catch (Exception ex) {
    throw ex;
  }
}

但在同一行执行后仍然出现另一个错误,403(禁止)。

对于上次的测试,我之前做了以下步骤:

  1. 在控制台开发人员上将客户端 ID 生成为服务帐户
  2. 在 Google Admin 上授予对 https://apps-apis.google.com/a/feeds/emailsettings/2.0/ 客户端 ID 的正确访问权限

如果我做错了什么,如果有人能帮助我,我将不胜感激。

提前致谢。

干杯!

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    我今天因为同样的问题而发疯,但是对于同一 API 范围内的“RetrieveSendAs”,就在这一分钟解决了它!

    “初始化程序”需要一个带有站点管理员主要电子邮件的“用户”参数:

    ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(Globals.GaServiceEmail)
    {
        User   = "gapps-admin-login@example.org",
        Scopes = new[] { "https://apps-apis.google.com/a/feeds/emailsettings/2.0/" }
    }.FromCertificate(gaCertificate));
    

    您还需要从代码中删除旧的“service.setUserCredentials”行。

    【讨论】:

    • 非常感谢!这正是我所需要的……不多不少!干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多