You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

4 weeks ago
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace TaskManager.Controllers
{
public class CheryRDCSharedInventoryController : IDoExecute
{
public Task ExecuteAsync()
{
throw new NotImplementedException();
}
}
// 需安装 Newtonsoft.Json 包
public class TokenService
{
private readonly HttpClient _httpClient;
private readonly string _appKey;
private readonly string _appSecret; // 若有密钥需传入
public TokenService(string appKey, string appSecret)
{
_httpClient = new HttpClient();
_appKey = appKey;
_appSecret = appSecret;
}
public async Task<string> GetTokenAsync(string tokenUrl)
{
var parameters = new
{
app_key = _appKey,
app_secret = _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.access_token; // 假设返回字段为 access_token
}
}
}