🐛 Fixes afdian service

This commit is contained in:
LittleSheep 2025-06-22 02:35:12 +08:00
parent 6b0e5f919d
commit 516090a5f8

View File

@ -60,8 +60,10 @@ public class AfdianOidcService(
return userInfo; return userInfo;
} }
protected override async Task<OidcTokenResponse?> ExchangeCodeForTokensAsync(string code, protected override async Task<OidcTokenResponse?> ExchangeCodeForTokensAsync(
string? codeVerifier = null) string code,
string? codeVerifier = null
)
{ {
var config = GetProviderConfig(); var config = GetProviderConfig();
var client = HttpClientFactory.CreateClient(); var client = HttpClientFactory.CreateClient();
@ -78,37 +80,44 @@ public class AfdianOidcService(
var response = await client.PostAsync("https://afdian.com/api/oauth2/access_token", content); var response = await client.PostAsync("https://afdian.com/api/oauth2/access_token", content);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<OidcTokenResponse>(); return new OidcTokenResponse()
{
AccessToken = code,
ExpiresIn = 3600
};
} }
private async Task<OidcUserInfo> GetUserInfoAsync(string accessToken) private async Task<OidcUserInfo> GetUserInfoAsync(string accessToken)
{ {
var config = GetProviderConfig();
var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "client_id", config.ClientId },
{ "client_secret", config.ClientSecret },
{ "grant_type", "authorization_code" },
{ "code", accessToken },
{ "redirect_uri", config.RedirectUri },
});
var client = HttpClientFactory.CreateClient(); var client = HttpClientFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://discord.com/api/users/@me"); var request = new HttpRequestMessage(HttpMethod.Post, "https://afdian.com/api/oauth2/access_token");
request.Headers.Add("Authorization", $"Bearer {accessToken}");
var response = await client.SendAsync(request); var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
var discordUser = JsonDocument.Parse(json).RootElement; var afdianUser = JsonDocument.Parse(json).RootElement;
var userId = discordUser.GetProperty("id").GetString() ?? ""; var userId = afdianUser.GetProperty("user_id").GetString() ?? "";
var avatar = discordUser.TryGetProperty("avatar", out var avatarElement) ? avatarElement.GetString() : null; var avatar = afdianUser.TryGetProperty("avatar", out var avatarElement) ? avatarElement.GetString() : null;
return new OidcUserInfo return new OidcUserInfo
{ {
UserId = userId, UserId = userId,
Email = (discordUser.TryGetProperty("email", out var emailElement) ? emailElement.GetString() : null) ?? "", DisplayName = (afdianUser.TryGetProperty("name", out var nameElement)
EmailVerified = discordUser.TryGetProperty("verified", out var verifiedElement) && ? nameElement.GetString()
verifiedElement.GetBoolean(),
DisplayName = (discordUser.TryGetProperty("global_name", out var globalNameElement)
? globalNameElement.GetString()
: null) ?? "", : null) ?? "",
PreferredUsername = discordUser.GetProperty("username").GetString() ?? "", ProfilePictureUrl = avatar,
ProfilePictureUrl = !string.IsNullOrEmpty(avatar)
? $"https://cdn.discordapp.com/avatars/{userId}/{avatar}.png"
: "",
Provider = ProviderName Provider = ProviderName
}; };
} }