8 changed files with 244 additions and 367 deletions
@ -1,185 +0,0 @@ |
|||
using Newtonsoft.Json; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Net.Http; |
|||
using System.Net.Http.Headers; |
|||
using System.Text; |
|||
|
|||
using System.Threading.Tasks; |
|||
|
|||
|
|||
namespace TaskManager.Controllers |
|||
{ |
|||
public class TOKEN_CONTROLLER |
|||
{ |
|||
|
|||
private readonly HttpClient _httpClient; |
|||
private readonly string _appKey = "8EG566b9bedd2bf46d"; |
|||
private readonly string _appSecret = "48edc4425647425d87f806a1ba492580"; // 若有密钥需传入
|
|||
|
|||
public TOKEN_CONTROLLER() |
|||
{ |
|||
_httpClient = new HttpClient(); |
|||
|
|||
|
|||
|
|||
} |
|||
public async Task ExecuteAsync() |
|||
{ |
|||
try |
|||
{ |
|||
var retult = await GetTokenAsync("https://ediuat.mychery.com/prod-api/auth/public/login/appKey"); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
Console.WriteLine(ex.Message); |
|||
} |
|||
|
|||
} |
|||
|
|||
public async Task<string> GetTokenAsync(string tokenUrl) |
|||
{ |
|||
var parameters = new |
|||
{ |
|||
appKey = _appKey, |
|||
appSecret = _appSecret, // 按需传参
|
|||
// 其他参数如 grant_type、scope 等根据接口要求调整
|
|||
}; |
|||
|
|||
var content = new StringContent( |
|||
JsonConvert.SerializeObject(parameters), |
|||
Encoding.UTF8, |
|||
"application/json" |
|||
); |
|||
var response = await _httpClient.PostAsync(tokenUrl, content); |
|||
response.EnsureSuccessStatusCode(); // 抛异常处理错误
|
|||
var responseBody = await response.Content.ReadAsStringAsync(); |
|||
var result = JsonConvert.DeserializeObject<dynamic>(responseBody); |
|||
return result.data.access_token; // 假设返回字段为 access_token
|
|||
} |
|||
|
|||
|
|||
|
|||
} |
|||
|
|||
//private readonly string _appKey = "8EG566b9bedd2bf46d";
|
|||
//private readonly string _appSecret = "48edc4425647425d87f806a1ba492580";
|
|||
//private readonly string _tokenEndpoint = "https://ediuat.mychery.com/prod-api/auth/public/login/appKey";
|
|||
|
|||
//private readonly HttpClient _httpClient;
|
|||
//private readonly SemaphoreSlim _refreshLock = new SemaphoreSlim(1, 1);
|
|||
//private string _currentToken;
|
|||
//private DateTime _tokenExpiry;
|
|||
//private bool _disposed;
|
|||
|
|||
//public TokenServiceController()
|
|||
//{
|
|||
// _httpClient = new HttpClient();
|
|||
// _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|||
//}
|
|||
|
|||
//public async Task<string> GetTokenAsync(CancellationToken cancellationToken = default)
|
|||
//{
|
|||
// // 检查令牌是否存在且未过期(提前60秒刷新以防止边缘情况)
|
|||
// if (!string.IsNullOrEmpty(_currentToken) && _tokenExpiry > DateTime.UtcNow.AddSeconds(60))
|
|||
// {
|
|||
// return _currentToken;
|
|||
// }
|
|||
|
|||
// // 等待获取锁,确保只有一个线程刷新令牌
|
|||
// await _refreshLock.WaitAsync(cancellationToken);
|
|||
// try
|
|||
// {
|
|||
// // 再次检查,避免其他线程已经刷新了令牌
|
|||
// if (!string.IsNullOrEmpty(_currentToken) && _tokenExpiry > DateTime.UtcNow.AddSeconds(60))
|
|||
// {
|
|||
// return _currentToken;
|
|||
// }
|
|||
|
|||
// // 调用认证API获取新令牌
|
|||
// var tokenResponse = await FetchNewTokenAsync(cancellationToken);
|
|||
|
|||
// // 更新令牌和过期时间
|
|||
// _currentToken = tokenResponse.AccessToken;
|
|||
// _tokenExpiry = DateTime.UtcNow.AddSeconds(tokenResponse.ExpiresIn);
|
|||
|
|||
// return _currentToken;
|
|||
// }
|
|||
// finally
|
|||
// {
|
|||
// _refreshLock.Release();
|
|||
// }
|
|||
//}
|
|||
|
|||
//private async Task<TokenResponse> FetchNewTokenAsync(CancellationToken cancellationToken)
|
|||
//{
|
|||
// try
|
|||
// {
|
|||
// // 创建请求内容
|
|||
// var requestBody = new
|
|||
// {
|
|||
// appKey = _appKey,
|
|||
// appSecret = _appSecret
|
|||
// };
|
|||
|
|||
// var content = new StringContent(
|
|||
// JsonSerializer.Serialize(requestBody),
|
|||
// Encoding.UTF8,
|
|||
// "application/json");
|
|||
|
|||
// // 发送请求
|
|||
// var response = await _httpClient.PostAsync(_tokenEndpoint, content, cancellationToken);
|
|||
// response.EnsureSuccessStatusCode();
|
|||
|
|||
// // 解析响应
|
|||
// var jsonResponse = await response.Content.ReadAsStringAsync(cancellationToken);
|
|||
// var tokenResponse = JsonSerializer.Deserialize<TokenResponse>(
|
|||
// jsonResponse,
|
|||
// new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|||
|
|||
// if (tokenResponse == null || string.IsNullOrEmpty(tokenResponse.AccessToken))
|
|||
// {
|
|||
// throw new InvalidOperationException("Failed to retrieve access token.");
|
|||
// }
|
|||
|
|||
// return tokenResponse;
|
|||
// }
|
|||
// catch (Exception ex)
|
|||
// {
|
|||
// Console.WriteLine($"Token acquisition failed: {ex.Message}");
|
|||
// throw;
|
|||
// }
|
|||
//}
|
|||
|
|||
//// 令牌响应模型
|
|||
//private class TokenResponse
|
|||
//{
|
|||
// public string AccessToken { get; set; }
|
|||
// public int ExpiresIn { get; set; } = 3600; // 默认1小时
|
|||
//}
|
|||
|
|||
//public void Dispose()
|
|||
//{
|
|||
// Dispose(true);
|
|||
// GC.SuppressFinalize(this);
|
|||
//}
|
|||
|
|||
//protected virtual void Dispose(bool disposing)
|
|||
//{
|
|||
// if (!_disposed)
|
|||
// {
|
|||
// if (disposing)
|
|||
// {
|
|||
// _httpClient?.Dispose();
|
|||
// _refreshLock?.Dispose();
|
|||
// }
|
|||
// _disposed = true;
|
|||
// }
|
|||
//}
|
|||
|
|||
//public Task ExecuteAsync()
|
|||
//{
|
|||
// throw new NotImplementedException();
|
|||
//}
|
|||
} |
Loading…
Reference in new issue