首页  >  文章  >  后端开发  >  为什么我从 Twitter API 收到“错误的身份验证数据”(错误 215)?

为什么我从 Twitter API 收到“错误的身份验证数据”(错误 215)?

Barbara Streisand
Barbara Streisand原创
2024-10-25 04:53:02600浏览

Why am I getting

错误 215:来自 Twitter API 的错误身份验证数据

当尝试访问 Twitter 的 API 并试图检索与以下内容关联的关注者列表时对于特定用户,可能会遇到代码 215 的错误消息和“错误的身份验证数据”消息。

此特定错误代码的文档尚未提供,但可以提供解释:

错误码215表示API调用时使用的鉴权数据不正确或无效。要解决此问题,请确保:

  • 消费者密钥和消费者秘密正确且与在 Twitter 上注册的一致。
  • 令牌和令牌秘密有效且已获得授权
  • 按照随机数和时间戳值的 OAuth 规范,正确生成随机数和时间戳。

作为参考,一个简化的 PHP 代码片段实现了下面提供了 OAuth 1.0 身份验证并向 Twitter API 发出请求:

<code class="php">$token = 'YOUR_TOKEN';
$token_secret = 'YOUR_TOKEN_SECRET';
$consumer_key = 'CONSUMER_KEY';
$consumer_secret = 'CONSUMER_SECRET';

$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/followers/ids.json'; // api call path

$query = array( // query parameters
    'cursor' => '-1',
    'screen_name' => 'username'
);

$oauth = array(
    'oauth_consumer_key' => $consumer_key,
    'oauth_token' => $token,
    'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
    'oauth_timestamp' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_version' => '1.0'
);

// complete the OAuth 1.0 authentication process
// ...

// continue with making the API call</code>

以上是为什么我从 Twitter API 收到“错误的身份验证数据”(错误 215)?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn