代码之家  ›  专栏  ›  技术社区  ›  Fayre

没有ServiceAccount的Gmail OAUTH 2.0

  •  0
  • Fayre  · 技术社区  · 8 年前

    我试图发送一封具有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:“客户端未经授权使用此方法检索访问令牌。” 我需要授权我的客户,这是对的吗?只有G诉讼账户才可能?所以我必须支付一个管理员帐户或者删除OAUTH?

    1 回复  |  直到 8 年前
        1
  •  1
  •   jstedfast    8 年前

    你需要跟随 Google's instructions

    推荐文章