简体   繁体   中英

Twitter auto login using abraham twitteroauth library

I have twitter username and password. What I want is, when a client adds any new article from the admin side I want its url be auto twit. I dont want to bother client to login to twitter each time he adds an article. Is there any way to auto login using Abraham twitteroauth library. Thankyou

Twitter requires first to Authorize a client app, then you'll able to auto-tweet on certain occasions (ie publishing new article). For a detailed depiction you may take a look at this Twitter module for Chyrp that I've created. It uses the Abraham's Twitter oAuth library. There's also a clear example inside the abraham's library archive that may clear this up a bit.

On the other hand, the CMS/Blog you're using for your client's site, should provide hooks (callbacks) in order to know when a post is being created, in order to call the Tweet method accordingly.

Example from the linked Twitter module for Chyrp:

1) Authorize with 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 method (truncated).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM