Skip to content

Commit 1ed3a2d

Browse files
HavenDVclaude
andcommitted
fix: switch auth from PrepareRequest to Authorized hook
PrepareRequest on the parent client doesn't propagate to sub-clients (TranslateText, RephraseText, etc.), causing 401 errors. The Authorized hook modifies the shared Authorizations list so all sub-clients correctly send Authorization: DeepL-Auth-Key. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d6c0dbe commit 1ed3a2d

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

src/libs/DeepL/Extensions/DeepLClient.Auth.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@ namespace DeepL;
44

55
public partial class DeepLClient
66
{
7-
#pragma warning disable CA1822 // Mark members as static
8-
partial void PrepareRequest(
9-
global::System.Net.Http.HttpClient client,
10-
global::System.Net.Http.HttpRequestMessage request)
7+
partial void Authorized(
8+
global::System.Net.Http.HttpClient client)
119
{
12-
if (request.Headers.Authorization is { Scheme: "Bearer", Parameter: { } apiKey })
10+
// DeepL uses "Authorization: DeepL-Auth-Key <key>" instead of "Bearer".
11+
// Rewrite the scheme in the shared Authorizations list so all sub-clients
12+
// (TranslateText, RephraseText, etc.) send the correct header.
13+
for (var i = 0; i < Authorizations.Count; i++)
1314
{
14-
request.Headers.Authorization =
15-
new global::System.Net.Http.Headers.AuthenticationHeaderValue("DeepL-Auth-Key", apiKey);
15+
var auth = Authorizations[i];
16+
if (auth is { Type: "Http", Name: "Bearer" })
17+
{
18+
Authorizations[i] = new EndPointAuthorization
19+
{
20+
Type = auth.Type,
21+
Location = auth.Location,
22+
Name = "DeepL-Auth-Key",
23+
Value = auth.Value,
24+
};
25+
}
1626
}
1727
}
18-
#pragma warning restore CA1822 // Mark members as static
1928
}

0 commit comments

Comments
 (0)