correcciones ObtenerAccessTokenAsync

This commit is contained in:
2025-09-22 19:49:44 +02:00
parent 8699a31142
commit 8dade76b6d

View File

@@ -6,26 +6,42 @@ using System.Threading.Tasks;
public static class GmailTokenManager public static class GmailTokenManager
{ {
//public static async Task<string> ObtenerAccessTokenAsync(GmailConfig config)
//{
// var stream = new MemoryStream(config.ClientSecret);
// var credencial = await GoogleWebAuthorizationBroker.AuthorizeAsync(
// GoogleClientSecrets.FromStream(stream).Secrets,
// new[] { "https://mail.google.com/" },
// config.Usuario,
// CancellationToken.None,
// new FileDataStore(config.TokenFolder, true)
// );
// return await credencial.GetAccessTokenForRequestAsync();
//}
private static readonly SemaphoreSlim _tokenSemaphore = new SemaphoreSlim(1, 1);
public static async Task<string> ObtenerAccessTokenAsync(GmailConfig config) public static async Task<string> ObtenerAccessTokenAsync(GmailConfig config)
{ {
//var stream = new FileStream(config.ClientSecretPath, FileMode.Open, FileAccess.Read);
var stream = new MemoryStream(config.ClientSecret); var stream = new MemoryStream(config.ClientSecret);
//var credencial = await GoogleWebAuthorizationBroker.AuthorizeAsync( await _tokenSemaphore.WaitAsync();
// GoogleClientSecrets.FromStream(stream).Secrets, try
// new[] { "https://www.googleapis.com/auth/gmail.send" }, {
// config.Usuario, var credencial = await GoogleWebAuthorizationBroker.AuthorizeAsync(
// CancellationToken.None, GoogleClientSecrets.FromStream(stream).Secrets,
// new FileDataStore(config.TokenFolder, true) new[] { "https://mail.google.com/" },
//); config.Usuario,
var credencial = await GoogleWebAuthorizationBroker.AuthorizeAsync( CancellationToken.None,
GoogleClientSecrets.FromStream(stream).Secrets, new FileDataStore(config.TokenFolder, true)
new[] { "https://mail.google.com/" }, );
config.Usuario,
CancellationToken.None,
new FileDataStore(config.TokenFolder, true)
);
return await credencial.GetAccessTokenForRequestAsync(); return await credencial.GetAccessTokenForRequestAsync();
}
finally
{
_tokenSemaphore.Release();
}
} }
} }