没有 ServiceAccount 的 Gmails OAUTH 2.0

Gmails OAUTH 2.0 without ServiceAccount

我正在尝试发送带有 OAUTH 2.0 身份验证的电子邮件,如下所示:

        var certificate = new X509Certificate2(@"C:\path\to\my.p12", "mypassword", X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(new ServiceAccountCredential
            .Initializer("my@id.gserviceaccount.com")
        {
            Scopes = new[] { "https://mail.google.com/" },
            User = "scm.beartung@gmail.com"
        }.FromCertificate(certificate));

        bool result = await credential.RequestAccessTokenAsync(CancellationToken.None);


        // send email
        using (var client = new SmtpClient())
        {
            client.Connect("smtp.gmail.com", 587, false);
            client.Authenticate("user.name", credential.Token.AccessToken);
            client.Send(myMessage);
            client.Disconnect(true);
        }

但是当我运行这段代码时,我得到了 TokenResponseException:"Client is unauthorized to retrieve access tokens using this method." 我需要授权我的客户是对的吗,只有 G Suit 帐户才能做到这一点?所以我必须支付管理员帐户费用或删除 OAUTH?

您需要按照 Google's instructions 为您的应用程序获取 OAuth 2.0 凭据。