diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSnsExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSnsExtensions.cs index 198a0c02..ab362634 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSnsExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientExecuteSnsExtensions.cs @@ -151,13 +151,67 @@ namespace SKIT.FlurlHttpClient.Wechat.Api IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Get, "sns", "component", "jscode2session") .SetQueryParam("grant_type", request.GrantType) + .SetQueryParam("js_code", request.JsCode) .SetQueryParam("appid", request.AppId) .SetQueryParam("component_appid", request.ComponentAppId) - .SetQueryParam("component_access_token", request.ComponentAccessToken) - .SetQueryParam("js_code", request.JsCode); + .SetQueryParam("component_access_token", request.ComponentAccessToken); return await client.SendRequestWithJsonAsync(flurlReq, data: request, cancellationToken: cancellationToken); } + + /// + /// 异步调用 [GET] /sns/oauth2/component/access_token 接口。 + /// REF: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html + /// + /// + /// + /// + /// + public static async Task ExecuteSnsOAuth2ComponentAccessTokenAsync(this WechatApiClient client, Models.SnsOAuth2ComponentAccessTokenRequest request, CancellationToken cancellationToken = default) + { + if (client is null) throw new ArgumentNullException(nameof(client)); + if (request is null) throw new ArgumentNullException(nameof(request)); + + if (request.ComponentAppId == null) + request.ComponentAppId = client.Credentials.AppId; + + IFlurlRequest flurlReq = client + .CreateRequest(request, HttpMethod.Get, "sns", "oauth2", "component", "access_token") + .SetQueryParam("grant_type", request.GrantType) + .SetQueryParam("code", request.Code) + .SetQueryParam("appid", request.AppId) + .SetQueryParam("component_appid", request.ComponentAppId) + .SetQueryParam("component_access_token", request.ComponentAccessToken); + + return await client.SendRequestWithJsonAsync(flurlReq, data: request, cancellationToken: cancellationToken); + } + + /// + /// 异步调用 [GET] /sns/oauth2/component/refresh_token 接口。 + /// REF: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html + /// + /// + /// + /// + /// + public static async Task ExecuteSnsOAuth2ComponentRefreshTokenAsync(this WechatApiClient client, Models.SnsOAuth2ComponentRefreshTokenRequest request, CancellationToken cancellationToken = default) + { + if (client is null) throw new ArgumentNullException(nameof(client)); + if (request is null) throw new ArgumentNullException(nameof(request)); + + if (request.ComponentAppId == null) + request.ComponentAppId = client.Credentials.AppId; + + IFlurlRequest flurlReq = client + .CreateRequest(request, HttpMethod.Get, "sns", "oauth2", "component", "refresh_token") + .SetQueryParam("grant_type", request.GrantType) + .SetQueryParam("refresh_token", request.RefreshToken) + .SetQueryParam("appid", request.AppId) + .SetQueryParam("component_appid", request.ComponentAppId) + .SetQueryParam("component_access_token", request.ComponentAccessToken); + + return await client.SendRequestWithJsonAsync(flurlReq, data: request, cancellationToken: cancellationToken); + } #endregion } } diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsComponentJsCode2SessionRequest.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsComponentJsCode2SessionRequest.cs index 84477e2e..4e635d10 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsComponentJsCode2SessionRequest.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsComponentJsCode2SessionRequest.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; - -namespace SKIT.FlurlHttpClient.Wechat.Api.Models +namespace SKIT.FlurlHttpClient.Wechat.Api.Models { /// /// 表示 [GET] /sns/component/jscode2session 接口的请求。 diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentAccessTokenRequest.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentAccessTokenRequest.cs new file mode 100644 index 00000000..ecc98f91 --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentAccessTokenRequest.cs @@ -0,0 +1,29 @@ +namespace SKIT.FlurlHttpClient.Wechat.Api.Models +{ + /// + /// 表示 [GET] /sns/oauth2/component/access_token 接口的请求。 + /// + public class SnsOAuth2ComponentAccessTokenRequest : SnsOAuth2AccessTokenRequest + { + /// + /// 获取或设置微信 AppId。 + /// + [Newtonsoft.Json.JsonIgnore] + [System.Text.Json.Serialization.JsonIgnore] + public string AppId { get; set; } = string.Empty; + + /// + /// 获取或设置第三方平台 AppId。如果不指定将使用构造 时的 参数。 + /// + [Newtonsoft.Json.JsonIgnore] + [System.Text.Json.Serialization.JsonIgnore] + public string? ComponentAppId { get; set; } + + /// + /// 获取或设置第三方平台 AccessToken。 + /// + [Newtonsoft.Json.JsonIgnore] + [System.Text.Json.Serialization.JsonIgnore] + public string ComponentAccessToken { get; set; } = string.Empty; + } +} diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentAccessTokenResponse.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentAccessTokenResponse.cs new file mode 100644 index 00000000..c008426c --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentAccessTokenResponse.cs @@ -0,0 +1,9 @@ +namespace SKIT.FlurlHttpClient.Wechat.Api.Models +{ + /// + /// 表示 [GET] /sns/oauth2/component/access_token 接口的响应。 + /// + public class SnsOAuth2ComponentAccessTokenResponse : SnsOAuth2AccessTokenResponse + { + } +} diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentRefreshTokenRequest.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentRefreshTokenRequest.cs new file mode 100644 index 00000000..0e0daac0 --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentRefreshTokenRequest.cs @@ -0,0 +1,29 @@ +namespace SKIT.FlurlHttpClient.Wechat.Api.Models +{ + /// + /// 表示 [GET] /sns/oauth2/component/refresh_token 接口的请求。 + /// + public class SnsOAuth2ComponentRefreshTokenRequest : SnsOAuth2RefreshTokenRequest + { + /// + /// 获取或设置微信 AppId。 + /// + [Newtonsoft.Json.JsonIgnore] + [System.Text.Json.Serialization.JsonIgnore] + public string AppId { get; set; } = string.Empty; + + /// + /// 获取或设置第三方平台 AppId。如果不指定将使用构造 时的 参数。 + /// + [Newtonsoft.Json.JsonIgnore] + [System.Text.Json.Serialization.JsonIgnore] + public string? ComponentAppId { get; set; } + + /// + /// 获取或设置第三方平台 AccessToken。 + /// + [Newtonsoft.Json.JsonIgnore] + [System.Text.Json.Serialization.JsonIgnore] + public string ComponentAccessToken { get; set; } = string.Empty; + } +} diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentRefreshTokenResponse.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentRefreshTokenResponse.cs new file mode 100644 index 00000000..2d871881 --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Models/Sns/Component/SnsOAuth2ComponentRefreshTokenResponse.cs @@ -0,0 +1,9 @@ +namespace SKIT.FlurlHttpClient.Wechat.Api.Models +{ + /// + /// 表示 [GET] /sns/oauth2/component/refresh_token 接口的响应。 + /// + public class SnsOAuth2ComponentRefreshTokenResponse : SnsOAuth2RefreshTokenResponse + { + } +}