Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6148949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:22:12+00:00 2026-05-23T19:22:12+00:00

Im using the following code to read to consumer_key and consumer_secret from config.php, pass

  • 0

Im using the following code to read to consumer_key and consumer_secret from config.php, pass it to twitter and retrieve some bits of information back from them.

What the script below attempts to do is ‘cache’ the request_token and request_secret. So in theory I should be able to reuse those details (all 4 of them to automatically tweet when required).

<?php

require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

    $consumer_key = CONSUMER_KEY;
    $consumer_secret = CONSUMER_SECRET; 

if (isset($_GET["register"]))
{
    // If the "register" parameter is set we create a new TwitterOAuth object
    // and request a token

    /* Build TwitterOAuth object with client credentials. */

    $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $request = $oauth->getRequestToken();

    $request_token = $request["oauth_token"];
    $request_token_secret = $request["oauth_token_secret"];

    // At this I store the two request tokens somewhere.

    file_put_contents("request_token", $request_token);
    file_put_contents("request_token_secret", $request_token_secret);

    // Generate a request link and output it
    $request_link = $oauth->getAuthorizeURL($request);
    echo "Request here: <a href=\"" . $request_link . "\">" . $request_link . "</a>";
    die();
}
elseif (isset($_GET["validate"]))
{
    // This is the validation part. I read the stored request
    // tokens.

    $request_token = file_get_contents("request_token");
    $request_token_secret = file_get_contents("request_token_secret");

    // Initiate a new TwitterOAuth object. This time we provide them with more details:
    // The request token and the request token secret

    $oauth = new TwitterOAuth($consumer_key, $consumer_secret,
        $request_token, $request_token_secret);

    // Ask Twitter for an access token (and an access token secret)
    $request = $oauth->getAccessToken();

    // There we go
    $access_token = $request['oauth_token'];
    $access_token_secret = $request['oauth_token_secret'];

    // Now store the two tokens into another file (or database or whatever):
    file_put_contents("access_token", $access_token);
    file_put_contents("access_token_secret", $access_token_secret);

    // Great! Now we've got the access tokens stored.
    // Let's verify credentials and output the username.
    // Note that this time we're passing TwitterOAuth the access tokens.


    $oauth = new TwitterOAuth($consumer_key, $consumer_secret,
        $access_token, $access_token_secret);

    // Send an API request to verify credentials
    $credentials = $oauth->oAuthRequest('https://twitter.com/account/verify_credentials.xml', 'GET', array());

    // Parse the result (assuming you've got simplexml installed)
    $credentials = simplexml_load_string($credentials);

    var_dump($credentials);

    // And finaly output some text
    echo "Access token saved! Authorized as @" . $credentials->screen_name;
    die();
}
?>

When i run /?verify&oauth_token=0000000000000000 – It works however trying to resuse the generated tokens etc… I get a 401

Here is the last bit of code where I attempt to reuse the details from Twitter combined with my consumer_key and consumer_secret and get the 401:

require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

// Read the access tokens
$access_token = file_get_contents("access_token");
$access_token_secret = file_get_contents("access_token_secret");

// Initiate a TwitterOAuth using those access tokens
$oauth = new TwitterOAuth($consumer_key, $consumer_key_secret,
    $access_token, $access_token_secret);

// Post an update to Twitter via your application:
$oauth->OAuthRequest('https://twitter.com/statuses/update.xml',
    array('status' => "Hey! I'm posting via #OAuth!"), 'POST');

Not sure whats going wrong, are you able to cache the details or do i need to try something else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:22:13+00:00Added an answer on May 23, 2026 at 7:22 pm

    You can’t store the OAuth tokens in cache and to more than 1 request with it, as OAuth is there to help make the system secure, your “oauth_token” will contain some unique data, this token will only be able to make one call back to twitter, as soon as the call was made, that “oauth_token” is no longer valid, and the OAuth class should request a new “oauth_token”, thus making sure that every call that was made is secure.

    That is why you are getting an “401 unauthorized” error on the second time as the token is no longer valid.

    twitter is still using OAuth v1 (v2 is still in the draft process even though facebook and google already implemented it in some parts)
    The image below describes the flow of the OAuth authentication.
    Hope it helps.

    OAuth authentication flow

    A while ago I used this to connect to twitter and send tweets, just note that it did make use of some Zend classes as the project was running on a zend server.

    require_once 'Zend/Service/Twitter.php';
    class Twitter {
    
        protected $_username = '<your_twitter_username>';
        protected $_token = '<your_twitter_access_token>';
        protected $_secret = '<your_twitter_access_token_secret>';
        protected $_twitter = NULL;
    
        //class constructor
        public function __construct() {
            $this->getTwitter();
        }
    
        //singleton twitter object   
        protected function getTwitter() {
            if (null === $this->_twitter) {
                $accessToken = new Zend_Oauth_Token_Access;
                $accessToken->setToken($this->_token)
                        ->setTokenSecret($this->_secret);
    
                $this->_twitter = new Zend_Service_Twitter(array(
                            'username' => $this->_username,
                            'accessToken' => $accessToken,
                        ));
    
                $response = $this->_twitter->account->verifyCredentials();
                if ($response->isError()) {
                    throw new Zend_Exception('Provided credentials for Twitter log writer are wrong');
                }
            }
            return $this->_twitter;
        }
    
        //send a status message to twitter
        public function update( $tweet ) {
            $this->getTwitter()->status->update($tweet);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following code from here to read an Excel file using
I'm using the following code within the JCProperty class to retrieve data from a
I'm using the following code to try to read the results of a df
I am using the following code to attempt to read a large file (280Mb)
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
I'm using the following code to query a database from my jsp, but I'd
I am using following PHP code to connect to MS Access database: $odb_conn =
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I've been using the following code to open Office Documents, PDF, etc. on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.