Twitter使用亚伯拉罕twitteroauth库自动登录

时间:2012-08-03 05:29:08

标签: php twitter

我有推特用户名和密码。我想要的是,当客户端从管理端添加任何新文章时,我希望它的URL是自动twit。每次他添加一篇文章时,我都不想打扰客户登录Twitter。有没有办法使用亚伯拉罕twitteroauth库自动登录。三江源

1 个答案:

答案 0 :(得分:0)

Twitter要求首先授权客户端应用,然后您就可以在某些情况下自动发推文(即发布新文章)。有关详细描述,您可以查看我创建的Twitter module for Chyrp。它使用亚伯拉罕的Twitter oAuth库。在亚伯拉罕的图书馆档案中也有一个明显的例子可能会清除这一点。

另一方面,您用于客户网站的CMS /博客应该提供挂钩(回调)以便知道何时创建帖子,以便相应地调用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)Tweet-it方法(截断)。

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;
}