简体   繁体   中英

OAuth-PHP and Twitter

I am trying to use OAuth-PHP to post a tweet using PHP, but I can't seem to figure out how to achieve that. Does anyone have any tips on how to do this, or where I should look?

I have seen the example that comes with the OAuth-PHP library, but I am not sure how to adapt it to post a tweet.

I have found a similar question with no answer so far at: Sending a tweet with oauth-php?

The code I am using is this:

    define("TWITTER_CONSUMER_KEY", $this->config->Twitter->ConsumerKey);
    define("TWITTER_CONSUMER_SECRET", $this->config->Twitter->ConsumerSecret);

    define("TWITTER_OAUTH_HOST","https://api.twitter.com/");
    define("TWITTER_REQUEST_TOKEN_URL", TWITTER_OAUTH_HOST . "oauth/request_token");
    define("TWITTER_AUTHORIZE_URL", TWITTER_OAUTH_HOST . "oauth/authorize");
    define("TWITTER_ACCESS_TOKEN_URL", TWITTER_OAUTH_HOST . "oauth/access_token");
    define("TWITTER_PUBLIC_TIMELINE_API", TWITTER_OAUTH_HOST . "statuses/public_timeline.json");
    define("TWITTER_UPDATE_STATUS_API", TWITTER_OAUTH_HOST . "statuses/update.json");

    define('OAUTH_TMP_DIR', function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : realpath($_ENV["TMP"]));

    // Twitter test
    $options = array('consumer_key' => TWITTER_CONSUMER_KEY, 'consumer_secret' => TWITTER_CONSUMER_SECRET);
    OAuthStore::instance("2Leg", $options);

    try {
        $request = new OAuthRequester(TWITTER_UPDATE_STATUS_API, "POST", array('status' => $msg));
        $result = $request->doRequest();
    }
    catch(OAuthException2 $e) {
        echo "Exception: " . $e->getMessage();
    }

This is what I am trying to do, but I get an exception: Request failed with code 401: {"request":"/statuses/update.json","error":"Invalid / expired Token"}

As I am new to OAuth, I might be missing an important step and not know it...

Any clues?

Any help will be appreciated.

Nevermind, I just used another OAuth Implementation instead, and a twitter oauth library.

It's posting, that's what matters.

I just wanted an independent library I could use for several social networks, I will try to use this one to achieve the same thing with facebook.

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