feat(openai): 新增获取令牌相关接口

This commit is contained in:
Fu Diwei
2021-10-08 11:05:07 +08:00
parent b8bcd8f2e7
commit d9c0381991
6 changed files with 114 additions and 1 deletions

View File

@@ -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);
}
}
}

View File

@@ -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";
}
}

View File

@@ -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!;
}
}
}
}

View File

@@ -29,7 +29,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
public virtual string? BotId { get; set; } public virtual string? BotId { get; set; }
/// <summary> /// <summary>
/// 获取或设置请求令牌。 /// 获取或设置接口访问令牌。
/// </summary> /// </summary>
[Newtonsoft.Json.JsonIgnore] [Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore] [System.Text.Json.Serialization.JsonIgnore]

View File

@@ -0,0 +1,14 @@
{
"openid": "eafc6f616a3f0667800e0f92dc00ee88167bc010",
"resources": [
"bot",
"common",
"dict",
"word",
"skill",
"intent",
"openapi",
"skill_store"
],
"authorization_type": "client_credentials"
}

View File

@@ -0,0 +1,8 @@
{
"code": 0,
"data": {
"access_token": "MX6ddM5mN07ucVKy+Y-to7tKRufZ1YF05eb542d5170000001c"
},
"msg": "success",
"request_id": "54ae04cf-5e95-44fd-ad3f-62e7b163836b"
}