我有twitter用户名和密码。我想要的是,当客户端从管理端添加任何新文章时,我希望它的url是自动twit。我不想每次客户添加文章时都麻烦他登录推特。有没有任何方法可以使用Abraham twitteroauth库自动登录。感谢
Twitter首先需要授权客户端应用程序,然后您才能在某些情况下自动推特(即发布新文章)。关于详细的描述,你可以看看我为Chyrp创建的这个推特模块。它使用亚伯拉罕的推特oAuth库。在亚伯拉罕的图书馆档案中也有一个明显的例子,可以稍微澄清一下。
另一方面,你为客户的网站使用的CMS/Blog应该提供挂钩(回调),以便知道何时创建帖子,从而相应地调用Tweet方法。
Chyrp:的链接Twitter模块示例
1) 使用Twitter授权:
static function admin_chweet_auth($admin) {
if (!Visitor::current()->group->can("change_settings"))
show_403(__("Access Denied"), __("You do not have sufficient privileges to change settings."));
# If the oauth_token is old redirect
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token'])
Flash::warning(__("Old token. Please refresh the page and try again."), "/admin/?action=chweet_settings");
# New TwitteroAuth object with app key/secret and token key/secret from SESSION
$tOAuth = new TwitterOAuth(C_KEY, C_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$access_token = $tOAuth->getAccessToken($_REQUEST['oauth_verifier']);
# Save access tokens locally for future tweeting.
$config = Config::current();
$config->set("chweet_oauth_token", $access_token["oauth_token"]);
$config->set("chweet_oauth_secret", $access_token["oauth_token_secret"]);
$config->set("chweet_user_id", $access_token["user_id"]);
$config->set("chweet_username", $access_token["screen_name"]);
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
if (200 == $tOAuth->http_code)
Flash::notice(__("Chweet was successfully Authorized to Twitter."), "/admin/?action=chweet_settings");
else
Flash::warning(__("Chweet couldn't be authorized."), "/admin/?action=chweet_settings");
}
2) add_post (trigger)
public function add_post($post) {
fallback($chweet, (int) !empty($_POST['chweet']));
SQL::current()->insert("post_attributes",
array("name" => "tweeted",
"value" => $chweet,
"post_id" => $post->id));
if ($chweet and $post->status == "public")
$this->tweet_post($post);
}
3) 推特方式(截断)。
public function tweet_post($post) {
$tOAuth = new TwitterOAuth(C_KEY, C_SECRET, $config->chweet_oauth_token, $config->chweet_oauth_secret);
$user = $tOAuth->get("account/verify_credentials");
$response = $tOAuth->post("statuses/update", array("status" => $status));
return $response;
}