From 4b9d46bbbe7f39fe9e10caed1fefc9aab0339f34 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Thu, 17 Jun 2021 19:44:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(wxads):=20=E4=BB=A5=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8=E5=BD=A2=E5=BC=8F=E9=87=8D=E6=96=B0=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=20AgencyToken=20=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WechatAdsAgencyTokenInterceptor.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/SKIT.FlurlHttpClient.Wechat.Ads/Interceptors/WechatAdsAgencyTokenInterceptor.cs diff --git a/src/SKIT.FlurlHttpClient.Wechat.Ads/Interceptors/WechatAdsAgencyTokenInterceptor.cs b/src/SKIT.FlurlHttpClient.Wechat.Ads/Interceptors/WechatAdsAgencyTokenInterceptor.cs new file mode 100644 index 00000000..3edf53ce --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Ads/Interceptors/WechatAdsAgencyTokenInterceptor.cs @@ -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(); + } + } +}