文档获取接口调用凭据 | 微信开放文档
HTTPS 调用
GET https://api.weixin.qq.com/cgi-bin/token
public async Task<GetAccessTokenDto> GetAccessToken()
{
var str = await distributedCache.GetOrAddAsync(WxAccessToken, async () =>
{
using (var client = httpClientFactory.CreateClient(WaterConstant.WxHttpClientName))
{
var body = $"?grant_type=client_credential&appid={options.Appid}&secret={options.Secret}";
var url = options.Url + WaterConstant.GetToken + body;
var responseMessage = await client.GetAsync(url);
return await responseMessage.Content.ReadAsStringAsync();
}
}, () => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(7000),
});
return JsonConvert.DeserializeObject<GetAccessTokenDto>(str) ?? new GetAccessTokenDto();
}
public class GetAccessTokenDto
{
/// <summary>
/// 获取到的凭证
/// </summary>
[JsonProperty("access_token")]
public string AccessToken { get; set; }
/// <summary>
/// 凭证有效时间,单位:秒。目前是7200秒之内的值。
/// </summary>
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
/// <summary>
/// 错误码
/// </summary>
[JsonProperty("errcode")]
public int Errcode { get; set; }
/// <summary>
/// 错误信息
/// </summary>
[JsonProperty("errmsg")]
public string Errmsg { get; set; }
}