feat(wxapi): 新增代公众号发起网页授权相关接口

This commit is contained in:
Fu Diwei 2022-01-04 00:38:56 +08:00
parent 21c7e1d6e3
commit 7764fe2144
6 changed files with 133 additions and 6 deletions

View File

@ -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<Models.SnsComponentJsCode2SessionResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
}
/// <summary>
/// <para>异步调用 [GET] /sns/oauth2/component/access_token 接口。</para>
/// <para>REF: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.SnsOAuth2ComponentAccessTokenResponse> 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<Models.SnsOAuth2ComponentAccessTokenResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
}
/// <summary>
/// <para>异步调用 [GET] /sns/oauth2/component/refresh_token 接口。</para>
/// <para>REF: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.SnsOAuth2ComponentRefreshTokenResponse> 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<Models.SnsOAuth2ComponentRefreshTokenResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
}
#endregion
}
}

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [GET] /sns/component/jscode2session 接口的请求。</para>

View File

@ -0,0 +1,29 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [GET] /sns/oauth2/component/access_token 接口的请求。</para>
/// </summary>
public class SnsOAuth2ComponentAccessTokenRequest : SnsOAuth2AccessTokenRequest
{
/// <summary>
/// 获取或设置微信 AppId。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string AppId { get; set; } = string.Empty;
/// <summary>
/// 获取或设置第三方平台 AppId。如果不指定将使用构造 <see cref="WechatApiClient"/> 时的 <see cref="WechatApiClientOptions.AppId"/> 参数。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string? ComponentAppId { get; set; }
/// <summary>
/// 获取或设置第三方平台 AccessToken。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string ComponentAccessToken { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,9 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [GET] /sns/oauth2/component/access_token 接口的响应。</para>
/// </summary>
public class SnsOAuth2ComponentAccessTokenResponse : SnsOAuth2AccessTokenResponse
{
}
}

View File

@ -0,0 +1,29 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [GET] /sns/oauth2/component/refresh_token 接口的请求。</para>
/// </summary>
public class SnsOAuth2ComponentRefreshTokenRequest : SnsOAuth2RefreshTokenRequest
{
/// <summary>
/// 获取或设置微信 AppId。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string AppId { get; set; } = string.Empty;
/// <summary>
/// 获取或设置第三方平台 AppId。如果不指定将使用构造 <see cref="WechatApiClient"/> 时的 <see cref="WechatApiClientOptions.AppId"/> 参数。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string? ComponentAppId { get; set; }
/// <summary>
/// 获取或设置第三方平台 AccessToken。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string ComponentAccessToken { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,9 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [GET] /sns/oauth2/component/refresh_token 接口的响应。</para>
/// </summary>
public class SnsOAuth2ComponentRefreshTokenResponse : SnsOAuth2RefreshTokenResponse
{
}
}