【问题标题】:Gmail API Set Forwarding with Service Account使用服务帐户的 Gmail API 集转发
【发布时间】:2019-01-15 00:11:27
【问题描述】:

在调用 Gmail API 设置转发地址时遇到错误的请求失败前置条件错误,谁能提供帮助?下面是我正在尝试使用的 C# .Net 控制台应用程序。我已将域范围的权限委托给服务帐户。

错误:

Google.Apis.Requests.RequestError 错误请求 [400] 错误 [ Message[Bad Request] Location[ - ] Reason[failedPrecondition] Domain[global] ]

我想我错过了模拟用户。所以,我添加了用户,现在我收到以下错误。

错误:

错误:“unauthorized_client”,描述:“客户端未经授权无法使用此方法检索访问令牌。”,Uri:“”

namespace GmailForwarder
{
    class Program
    {

        static string ApplicationName = "GmailForwarder";

        static void Main(string[] args)
        {
            ServiceAccountCredential credential;
            string serviceAccount = "gmailforwarder@gmailforwarder.iam.gserviceaccount.com";
            var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);

            try
            {
                // Create credential
                credential = new ServiceAccountCredential(
                          new ServiceAccountCredential.Initializer(serviceAccount)
                          {
                            User = "wtestboonew@chicagobooth.edu",
                            Scopes = new[] { GmailService.Scope.GmailSettingsSharing  }
                          }.FromCertificate(certificate));

                // Create Gmail API service.
                var service = new GmailService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });

                string user = "wtestboonew@chicagobooth.edu"; // test gmail account
                string fwdAddr = "acw5274@gmail.com";

                ForwardingAddress fwdAddress = new ForwardingAddress();
                fwdAddress.ForwardingEmail = fwdAddr;

                var createFwdAddressResult = service.Users.Settings.ForwardingAddresses.Create(fwdAddress,"me").Execute();     

            }
            catch (Exception ex)
            {

            }     
        }
    }
} 

【问题讨论】:

  • 你能发布完整的错误信息吗?
  • DalmTo 你能帮我解决这个问题吗?我有什么遗漏,做错了吗?
  • 确保您在 gsuite 中正确设置了域范围的委派。那么您可以使用 Gmail api 服务
  • 是的,我的系统管理员和我仔细检查了这一点,并为服务帐户设置了这一点。服务帐户的所有者(即我们在其下创建服务帐户的 google 管理员)是否需要启用域范围的委派?当我尝试通过 Google API 资源管理器运行它时,它说客户端(即服务帐户的所有者没有启用委派)。这与我在运行 .Net 应用程序时遇到的错误相同吗?
  • 有没有办法以另一种方式测试服务帐户?我相信代码是正确的,但服务帐户没有访问权限,即使我在 G Suite 管理控制台中的 API Client Access 下注册了它

标签: c# google-api gmail-api google-api-dotnet-client service-accounts


【解决方案1】:

当我使用 https://mail.google.com/ 和 https://www.googleapis.com/auth/gmail.settings.sharing OAuth2 范围时,这对我有用

【讨论】:

    【解决方案2】:

    我也遇到过这个问题(400 代码)。我遇到了两个问题:

    1) 正如 ACW 所说,我在所需范围列表中缺少“https://mail.google.com/”,来自“https://www.googleapis.com/auth/gmail.settings.sharing”。

    2) 我没有在管理控制台中将范围用引号 ("") 括起来(在为服务帐户指定范围时)。

    希望对某人有所帮助

    【讨论】:

      猜你喜欢
      • 2014-09-13
      • 2022-01-15
      • 2014-12-13
      • 2021-03-15
      • 2021-06-22
      • 2020-09-23
      • 2016-12-19
      • 2020-09-30
      • 2015-03-04
      相关资源
      最近更新 更多