mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-12-29 18:04:42 +08:00
feat(openai): 新增获取令牌相关接口
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl;
|
||||
using Flurl.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
{
|
||||
public static class WechatOpenAIClientExecuteTokenExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>异步调用 [POST] /v1/token 接口。</para>
|
||||
/// <para>REF: https://developers.weixin.qq.com/doc/aispeech/openapi/api/v1/get_token.html </para>
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<Models.TokenResponse> ExecuteTokenAsync(this WechatOpenAIClient client, Models.TokenRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (client is null) throw new ArgumentNullException(nameof(client));
|
||||
if (request is null) throw new ArgumentNullException(nameof(request));
|
||||
|
||||
IFlurlRequest flurlReq = client
|
||||
.CreateRequest(request, HttpMethod.Post, "v1", "token")
|
||||
.WithHeader("Client-Id", client.Credentials.ClientId)
|
||||
.WithHeader("Client-Key", client.Credentials.ClientKey);
|
||||
|
||||
return await client.SendRequestWithJsonAsync<Models.TokenResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>表示 [POST] /v1/token 接口的请求。</para>
|
||||
/// </summary>
|
||||
public class TokenRequest : WechatOpenAIRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置用户唯一标识。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("openid")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("openid")]
|
||||
public string OpenId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置申请的资源权限列表。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("resources")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("resources")]
|
||||
public IList<string> ResourceList { get; set; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// <i>(使用默认值即可,无需修改)</i>
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("authorization_type")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("authorization_type")]
|
||||
public string AuthorizationType { get; set; } = "client_credentials";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>表示 [POST] /v1/token 接口的响应。</para>
|
||||
/// </summary>
|
||||
public class TokenResponse : WechatOpenAIResponse<TokenResponse.Types.Data>
|
||||
{
|
||||
public static class Types
|
||||
{
|
||||
public class Data
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置接口访问令牌。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("access_token")]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("access_token")]
|
||||
public string AccessToken { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
|
||||
public virtual string? BotId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置请求令牌。
|
||||
/// 获取或设置接口访问令牌。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
|
||||
Reference in New Issue
Block a user