invalid_authentication_error test failure - cc-switch
Quick fix
Upgrade cc-switch to fix failed {{apiKey}} parsing in custom usage query scripts, or temporarily hard-code the real API Key.
Symptom
Section titled “Symptom”测试失败: HTTP 401 Unauthorized : {"error":{"message":"Invalid Authentication","type":"invalid_authentication_error"}}Test failure: HTTP 401 Unauthorized : {"error":{"message":"Invalid Authentication","type":"invalid_authentication_error"}}
When configuring custom usage query scripts for providers such as Kimi in cc-switch, clicking “Test script” returns a 401 authentication error. In earlier versions, usage query script variable parsing is defective and cannot correctly recognize and replace template variables such as {{apiKey}} or {{baseUrl}}, causing an invalid Authorization Header to be sent to the official API. This issue affects only the test script path; the actual query path may work normally. The variable parsing logic was fixed in later versions, supporting multiple aliases and forms with spaces.
Upgrade cc-switch to v3.15.0 or later, such as v3.16.0, to get the fixed variable parsing engine. It now supports common forms such as {{apiKey}}, {{ apiKey }}, and {{api_key}}.
If you cannot upgrade yet, temporarily replace {{apiKey}} in the test script headers with the real API Key string for a hard-coded test.
// test.jsheaders: {Authorization: 'Bearer {{apiKey}}',Authorization: 'Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxx','User-Agent': 'cc-switch/1.0',}
Affected Versions
Section titled “Affected Versions”Source Issues
Section titled “Source Issues”This page is distilled from 2 real issues
- Why does replacing {{apiKey}} with the real Key make the test succeed?
- Because earlier versions of cc-switch have a variable parsing bug in the “Test script” feature and cannot correctly replace template variables, the request returns 401 due to missing authentication. Hard-coding bypasses variable parsing.
- Why does the test script fail, but the balance still displays normally on the home page?
- The test script path and the actual query path behave inconsistently. Even if variable parsing fails in the test script, variable parsing on the actual query path may work normally, so the home page can fetch and refresh the balance.
Related problems
Section titled “Related problems”This is an unofficial community wiki with no affiliation to the cc-switch authors or the project itself. Its content is compiled from the project's public GitHub issues. This site distributes no software.