我正在尝试使用codebird使用PHP进行推文。最初我无法获得访问令牌,但在我在设置中定义了CallbackURL后,问题似乎得到了解决。现在它返回oauth标记:
Codebird \ Codebird对象([_oauth_token:protected] => codehere [_oauth_token_secret:protected] => codehere [_return_format:protected] => 0 [ _supported_media_files:protected] =>数组([0] => 1 [1] => 2 [2] => 3)[_ version:protected] => 3.0.0-dev [_use_curl:protected] = > 1 [_timeout:protected] => 10000 [_connectionTimeout:protected] => 3000)
但是当我尝试发推文时,我得到以下错误:
stdClass对象([errors] =>数组([0] => stdClass对象([code] => 89 [message] =>令牌无效或过期。 ))[httpstatus] => 401 [rate] => )
以下是我的代码
Codebird\Codebird::setConsumerKey('copy+paste from twitter', 'copy+paste from twitter'); // I changed it to my settings
$cb = \Codebird\Codebird::getInstance();
if (! isset($_SESSION['oauth_token'])) {
// get the request token
$reply = $cb->oauth_requestToken(array(
'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
));
// store the token
$cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
$_SESSION['oauth_verify'] = true;
// redirect to auth website
$auth_url = $cb->oauth_authorize();
header('Location: ' . $auth_url);
die();
} elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
// verify the token
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
unset($_SESSION['oauth_verify']);
// get the access token
$reply = $cb->oauth_accessToken(array(
'oauth_verifier' => $_GET['oauth_verifier']
));
// store the token (which is different from the request token!)
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
// send to same URL, without oauth GET parameters
header('Location: ' . basename(__FILE__));
die();
}
// assign access token on each page load
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
print_r($cb);
$params = array(
'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter'
);
$reply = $cb->statuses_update($params);
print_r($reply);
提前感谢您的帮助。
答案 0 :(得分:1)
您的回调地址是在应用定义中使用Twitter注册的地址吗?
您是否在应用定义中拥有“读写”权限?
您是否超过了发布的费率限制?
我先检查这些内容,因为我的代码段中没有发现任何明显遗漏的内容。