Twitter 应用程序 oauth 开始返回错误代码 89 工作 1 年后令牌无效或过期


Twitter application oauth started returning error code 89 Invalid or expired token after 1 year working

我已经使用基于 Jon Hurlock 的 Twitter 仅限应用程序的身份验证应用程序的代码一年多了,没有问题,大约 2 天前,它在尝试生成持有者令牌时开始返回此错误:

令牌无效或过期,代码:89

我的代码略有改动,以强制它检查 SSL,因为该页面不在启用 SSL 的域上。 我在最新的cacert.pem文件中有卷曲拉动。

这是应用程序级别的 oauth,而不是个人 oauth。因此,每次进行调用时,我都会生成一个持有者令牌,进行 API 调用,然后使持有者令牌失效。 你可以在这里看到他的原始代码(我为我使用的部分提取了最新版本(: https://github.com/jonhurlock/Twitter-Application-Only-Authentication-OAuth-PHP/blob/master/Oauth.php

这是用于获取持有者令牌的代码。注意 我只需要包含应用程序的密钥和机密,不涉及任何用户,用户永远不必允许应用或对其进行身份验证:

    // Step 1
// step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738
$encoded_consumer_key = urlencode(CONSUMER_KEY);
$encoded_consumer_secret = urlencode(CONSUMER_SECRET);
// step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secret
$bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret;
// step 1.3 - base64-encode bearer token
$base64_encoded_bearer_token = base64_encode($bearer_token);
// step 2
$url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication
$headers = array( 
    "POST /oauth2/token HTTP/1.1", 
    "Host: api.twitter.com", 
    "User-Agent: Twitter App-Only Search",
    "Authorization: Basic ".$base64_encoded_bearer_token,
    "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
); 
$ch = curl_init();  // setup a curl
curl_setopt($ch, CURLOPT_URL,$url);  // set url to send to
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
curl_setopt($ch, CURLOPT_POST, 1); // send as post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, True);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, "/directory/path/cacert2014.pem");
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); 
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$retrievedhtml = curl_exec ($ch); // execute the curl
curl_close($ch); // close the curl
这是我

发现的(使用John Hurlock的相同库(。 应缓存持有者令牌。

这个特定的实现将为每个请求要求一个新的不记名令牌,Twitter 希望我们将缓存此令牌并返回错误,如果我们不这样做,有时会返回 403 禁止。

就我而言,我在网站上关闭了一段时间的推特,然后在缓存不记名令牌后将其重新打开。 现在一切都回来了。 你也可以在开发者控制台中切换到新的 Twitter 应用,而不是等待。

我在Memcache中缓存了我的令牌,但您可以选择做一些不同的事情。祝你好运!