我如何使用刷新令牌通过Google API(C#)生成新的身份验证令牌?

问题描述 投票:0回答:1

如果这是一个新手问题,请原谅我。以下代码在最初的一个小时内有效,然后访问令牌到期,并且不再起作用。我读过很多地方应该使用刷新令牌来更新访问令牌,但是我看不到如何完成的。我使用生成的访问令牌发送电子邮件。感谢您的帮助!


    private void GetClientSecrets()
    {
            // I don't like requesting this much access but I can't get it to work with less access
            string[] scopes = { "email", "profile", "https://mail.google.com/" };

            UserCredential credential;

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {
                    ClientId = _clientID,
                    ClientSecret = _clientSecret
                },
                scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(_credentialPath, true)).Result;

            var jwtPayload = GoogleJsonWebSignature.ValidateAsync(credential.Token.IdToken).Result;
            _username = jwtPayload.Email;
            _accessToken = credential.Token.AccessToken;

}

    public void SendGmail(string toEmail, string subject, string body, string replyTo = "", string cc = "", string bcc = "")
    {
            // Gmail SMTP server address
            SmtpServer oServer = new SmtpServer("smtp.gmail.com");
            // enable SSL connection
            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
            // Using 587 port, you can also use 465 port
            oServer.Port = 587;

            // use Gmail SMTP OAUTH 2.0 authentication
            oServer.AuthType = SmtpAuthType.XOAUTH2;
            // set user authentication
            oServer.User = _username;
            // use access token as password
            oServer.Password = _accessToken;
            oServer.MailFrom = _username;

            SmtpMail oMail = new SmtpMail("TryIt");
            oMail.From = _username;
            oMail.To = toEmail;
            if (!string.IsNullOrWhiteSpace(cc))
            {
                oMail.Cc = cc;
            }
            if (!string.IsNullOrWhiteSpace(replyTo))
            {
                oMail.ReplyTo = replyTo;
            }
            if (!string.IsNullOrWhiteSpace(bcc))
            {
                oMail.Bcc = bcc;
            }

            oMail.Subject = subject;
            oMail.TextBody = body;

            if (SystemConfig.DebugMode)
            {
                Logger.SimpleWriteToLog($"Sending email from {_username} (reply to: {replyTo}) to {toEmail} (CC: {cc}, BCC: {bcc}) using OAUTH 2.0{Environment.NewLine}{subject}{Environment.NewLine}{body}");
            }

            EASendMail.SmtpClient oSmtp = new EASendMail.SmtpClient();
            oSmtp.SendMail(oServer, oMail);
    }

}

UPDATE(2/12/2020)

我在API中找不到如何执行此操作,但此页面显示了如何使用POST执行此操作。

刷新访问令牌

https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline

c# email gmail token refresh
1个回答
0
投票

更新(2/12/2020)

我在API中找不到如何执行此操作,但此页面显示了如何使用POST执行此操作。

刷新访问令牌

https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline

© www.soinside.com 2019 - 2024. All rights reserved.