C# Gmail API使用服务帐户设置转发

C# Gmail API使用服务帐户设置转发,c#,google-api,gmail-api,google-api-dotnet-client,service-accounts,C#,Google Api,Gmail Api,Google Api Dotnet Client,Service Accounts,当我在调用Gmail API设置转发地址时遇到错误请求失败的前提条件错误,有人能帮我吗?下面是我正在尝试使用的一个C#.Net控制台应用程序。我已将整个域的权限委托给服务帐户 错误: Google.API.Requests.RequestError错误请求[400]错误[消息[错误请求]位置[-]原因[失败的前提条件]域[全局]] 我想我错过了要模拟的用户。因此,我添加了用户,现在我得到了以下错误 错误: 错误:“unauthorized_client”,说明:“客户端无权使用此方法检索访问令牌

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

错误

Google.API.Requests.RequestError错误请求[400]错误[消息[错误请求]位置[-]原因[失败的前提条件]域[全局]]

我想我错过了要模拟的用户。因此,我添加了用户,现在我得到了以下错误

错误

错误:“unauthorized_client”,说明:“客户端无权使用此方法检索访问令牌。”,Uri:“


当我使用OAuth2作用域时,这对我很有效:我也有这个问题(400代码)。我有两个问题:

1) 正如ACW所说,我在所需作用域列表中缺少了“”,而appart则来自“”

2) 在管理控制台中(指定服务帐户的作用域时),我没有将作用域用引号(“”)括起来


希望它能帮助别人

你能发布完整的错误消息吗?DalmTo你能帮我吗?有什么我遗漏的,做错了吗?请确保您在gsuite中正确设置了域范围的委派。然后你可以使用Gmail apiYea的服务,我的系统管理员和我仔细检查了这一点,这是为服务帐户设置的。服务帐户的所有者(即我们创建服务帐户的google管理员)是否需要启用域范围的delegationenabled?。当我试图通过谷歌API浏览器运行它时,它会显示客户端(即服务帐户的所有者没有启用委托)。这与我运行.Net应用程序时遇到的错误相同吗?是否有其他方法测试服务帐户?我相信代码是正确的,但是服务帐户没有访问权限,即使我在G Suite管理控制台的API Client access下注册了它
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)
            {

            }     
        }
    }
}