Clearer diagnostic when an API Key SID is passed without `account_sid`

未关闭 适合新手
#985 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@saryeljaramany 已经在做这个了。

开始于 2026年11月14日。

  • #990 来自 @saryeljaramany —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
api

调研方向

从twilio/base/client_base.py中account_sid的回退逻辑开始,并检查tests/unit下的相关单元测试。为不带account_sid的API Key SID添加测试覆盖,同时检查显式指定的account_sid和现有的令牌设置仍然有效。缺少Account SID的情况给出可采取行动的错误提示,且相关测试通过,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Location (twilio/twilio-python @ default branch 74b9629):

        self.account_sid = account_sid or self.username

Problem

The README shows API-key authentication as Client(api_key, api_secret, account_sid). If the third argument is forgotten, e.g. Client("SKxxxx...", "secret"), the constructor accepts it and ClientBase.__init__ falls back to self.account_sid = account_sid or self.username, so the API Key SID is used as the account SID in account-scoped resource paths. The fallback is silent: there is no validation or hint in the constructor, and the mistake only surfaces later as an authentication/not-found error from the API, which does not point at the missing account_sid.

Reproduction

from twilio.rest import Client

client = Client("SK" + "a" * 32, "my-api-secret")   # no error
print(client.account_sid)                            # 'SKaaaa...'
client.messages.create(...)                          # fails later with an API error

Environment:

  • Commit: 74b96296b0805986252b798bda4bca90341487ee (main, twilio 9.12.0)
  • Toolchain: Python 3.13.18

Workaround: always pass account_sid= explicitly when authenticating with API keys.

Already fixed in twilio/twilio-node

PR: https://github.com/twilio/twilio-node/pull/828 — "fix: give more descriptive error for accountSid init issues" (merged 2022-11-28T17:54:36Z, merge commit e18440d317c). When the SID used as accountSid starts with SK, the constructor error now says that an API Key requires the accountSid to be passed as an additional option. twilio-node already rejects non-AC account SIDs; that check is not being proposed here, only the API Key hint.

How twilio-node fixed it:

--- a/lib/base/BaseTwilio.ts
+++ b/lib/base/BaseTwilio.ts
@@ -88,7 +88,11 @@ export class BaseTwilio {
...
     if (!this.accountSid?.startsWith("AC")) {
-      throw new Error("accountSid must start with AC");
+      const apiKeyMsg = this.accountSid.startsWith("SK")
+        ? ". The given SID indicates an API Key which requires the accountSid to be passed as an additional option"
+        : "";
+
+      throw new Error("accountSid must start with AC" + apiKeyMsg);

Suggested fix

Mirroring twilio-node's API Key hint, ClientBase.__init__ raises a TwilioException with an actionable message when username starts with SK and neither account_sid nor a credential_provider is given; no AC prefix is enforced and other setups are unchanged:

diff --git a/twilio/base/client_base.py b/twilio/base/client_base.py
--- a/twilio/base/client_base.py
+++ b/twilio/base/client_base.py
@@ -4,6 +4,7 @@ from typing import Dict, List, MutableMapping, Optional, Tuple
 from urllib.parse import urlparse, urlunparse
 
 from twilio import __version__
+from twilio.base.exceptions import TwilioException
 from twilio.http import HttpClient
 from twilio.http.http_client import TwilioHttpClient
 from twilio.http.response import Response
@@ -54,6 +55,17 @@ class ClientBase(object):
         self.credential_provider = credential_provider or None
         """ :type : CredentialProvider """
 
+        if (
+            not account_sid
+            and not self.credential_provider
+            and self.username
+            and self.username.startswith("SK")
+        ):
+            raise TwilioException(
+                "The given username indicates an API Key SID, which requires "
+                "your Account SID to be passed via account_sid"
+            )
+
         self.account_sid = account_sid or self.username
         """ :type : str """
         self.auth = (self.username, self.password)

Verified on 74b9629 (Python 3.13.18, pytest 9.2.0): a unit test for the reproduction above (Client("SK...", "my-api-secret") must raise an error mentioning the API Key and account_sid, while Client("SK...", secret, account_sid="AC...") and Client("AC...", token) keep working) fails without this patch and passes with it; no other test results change in tests/unit.

I can submit a PR for this.

Checked: #259 / #286 (2016) were about API-key auth not being supported/how to pass the account SID; they predate the current account_sid parameter and don't cover diagnosing the omission.

主要语言
Python
星标
2.1k
派生
821
平均合并
9 小时 33 分钟
30 天内合并 PR
3

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

twilio/twilio-python 的其他 Issue

查看 twilio/twilio-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。