使用Twitter API v2获取用户信息遇client-not-enrolled错误求助
问题:调用Twitter API v2时出现"client-not-enrolled"错误
原本通过James Mallison的TwitterAPIExchange调用Twitter API 1.1获取指定用户最新推文的页面,上周起无法正常工作。改用Abraham\TwitterOAuth调用API v2请求用户信息时,持续收到"client-not-enrolled"错误。使用的是免费版API,不确定是权限限制还是操作有误,附上代码及错误结果。
代码示例
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret, $bearer_token); $connection->setApiVersion('2'); $response = $connection->get('users', ['ids' => ****]); print_r($response);
返回的错误结果
stdClass Object ( [client_id] => ****** [detail] => When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal. [title] => Client Forbidden [required_enrollment] => Appropriate Level of API Access [reason] => client-not-enrolled [type] => https://api.twitter.com/2/problems/client-forbidden )
解决办法
- API v2强制要求所有调用的App必须关联到一个Twitter开发者项目,免费版也不例外,这是触发错误的核心原因。
- 操作步骤:
- 登录Twitter开发者平台,进入「Projects & Apps」板块
- 若没有已创建的项目,点击创建新项目,按引导完成基础设置
- 将你当前使用的App绑定到这个新项目中
- 确认代码中使用的所有密钥(consumer_key、consumer_secret、access_token等)均来自已关联项目的App,避免使用未绑定项目的旧密钥
- 免费版API可正常调用
/users端点,绑定项目后即可解决该错误
内容的提问来源于stack exchange,提问作者tmtsantos
相关产品推荐
相关产品推荐

