mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-12-30 02:14:44 +08:00
refactor(wxads): 以拦截器形式重新实现自动生成 AgencyToken 的逻辑
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl;
|
||||
using Flurl.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Ads.Interceptors
|
||||
{
|
||||
internal class WechatAdsAgencyTokenInterceptor : WechatHttpCallInterceptor
|
||||
{
|
||||
private readonly string _agencyId;
|
||||
private readonly string _agencyApiKey;
|
||||
|
||||
public WechatAdsAgencyTokenInterceptor(string agencyId, string agencyApiKey)
|
||||
{
|
||||
_agencyId = agencyId;
|
||||
_agencyApiKey = agencyApiKey;
|
||||
}
|
||||
|
||||
public override async Task BeforeCallAsync(FlurlCall flurlCall)
|
||||
{
|
||||
if (flurlCall == null) throw new ArgumentNullException(nameof(flurlCall));
|
||||
|
||||
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
|
||||
string nonce = Guid.NewGuid().ToString("N");
|
||||
string sign = Security.MD5Utility.Hash($"{_agencyId}{timestamp}{nonce}{_agencyApiKey}").ToLower();
|
||||
string token = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_agencyId},{timestamp},{nonce},{sign}"));
|
||||
|
||||
flurlCall.Request.RemoveQueryParam("agency_token");
|
||||
flurlCall.Request.SetQueryParam("agency_token", token);
|
||||
|
||||
await Task.Yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user