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 6944551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:19:17+00:00 2026-05-27T13:19:17+00:00

I am trying to find a way to keep connected with the Twitter API

  • 0

I am trying to find a way to keep connected with the Twitter API once authorised using OAuth but am having problems.

I get "Invalid / expired Token" when trying to connect to Twitter API using a saved Oauth token in a session or database.

Is there a way to do this? I dont want the users of my App to have to login via Twitter every time. Surely once they have authorised my App once, that should be enough?

$consumer_key = 'consumerkey';
$consumer_secret = 'consumersecret';

$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);

if (isset($_GET['oauth_token'])){
    $oauth_token = $_GET['oauth_token'];
} else if ($_SESSION['oauth_token']){
    $oauth_token = $_SESSION['oauth_token'];

    echo $_SESSION['oauth_token'];
} else {
    //see if authorisation already set up in DB
    $query = mysql_query("SELECT oauth_token FROM PingSocialMediaUsers WHERE oauth_provider = 'twitter' AND clientID = '$clientID'");  
    $result = mysql_fetch_row($query); 
    $oauth_token = $result[0];
}

if($oauth_token == ''){ 
    $url = $twitterObj->getAuthorizationUrl();
    $twitter_login = $url;
 } else {
    $twitterObj->setToken($oauth_token);
    $token = $twitterObj->getAccessToken();
    $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);     
    $_SESSION['oauth_token'] = $token->oauth_token;
    $_SESSION['oauth_secret'] = $token->oauth_token_secret;
    $twitterInfo= $twitterObj->get_accountVerify_credentials();
    $twitterInfo->response;

    echo $twitterInfo->response['error'];
    //echo '<pre>';
    //print_r($twitterInfo);
    $id = $twitterInfo->id;
    $username = $twitterInfo->screen_name;

    //add to details database
    //find the user by ID  
    if ($id != ''){
        $query = mysql_query("SELECT * FROM PingSocialMediaUsers WHERE oauth_provider = 'twitter' AND oauth_uid = '$id'");  
        $result = mysql_fetch_array($query);  

        // If does not exist add to database  
        if(empty($result)){  
            $query = mysql_query("INSERT INTO PingSocialMediaUsers (oauth_provider, oauth_uid, username, oauth_token, oauth_secret) VALUES ('twitter', {$id}, '{$username}', '{$_SESSION['oauth_token']}', '{$_SESSION['oauth_secret']}')"); 
            $query = mysql_query("SELECT * FROM PingSocialMediaUsers WHERE id = " . mysql_insert_id());  
            $result = mysql_fetch_array($query);  
        } else {  
            //update the tokens  
            $query = mysql_query("UPDATE PingSocialMediaUsers SET oauth_token = '{$_SESSION['oauth_token']}', oauth_secret = '{$_SESSION['oauth_secret']}' WHERE oauth_provider = 'twitter' AND oauth_uid = {$id}");  
        }   

        $_SESSION['id'] = $result['id']; 
        $_SESSION['username'] = $result['username']; 
        $_SESSION['oauth_uid'] = $result['oauth_uid']; 
        $_SESSION['oauth_provider'] = $result['oauth_provider']; 
        $_SESSION['oauth_token'] = $result['oauth_token']; 
        $_SESSION['oauth_secret'] = $result['oauth_secret'];    
    }

    $twitterAuth = TRUE;    
} 
  • 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-27T13:19:18+00:00Added an answer on May 27, 2026 at 1:19 pm

    I believe you are talking about access_token which you will get from the twitter as last part of OAuth handshaking after which you can go to access there services on user behalf.

    Here is what they are saying in there official developer page

    We do not currently expire access tokens. 
    Your access token will be invalid if a user explicitly 
    rejects your application from their settings or if a Twitter admin suspends 
    your application. If your application is suspended there will be
    a note on your application page saying that it has been suspended.
    

    So you can very well store that token in your database and can always use at later stage.

    here is the reference to there API page
    Twitter OAuth FAQ

    So i suggest you to make sure that you are not changing any application setting and you are getting a valid access_token from session/database

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find a way to keep connected with the Facebook API
I'm trying to find a way to get the execution time of a section
I'm trying to find the best way to keep my clients updated as fast
I am designing my website and I'm trying to find a way to keep
I am trying to find a way to get the source code for (user
I am trying to find what the easiest way to keep form values after
I am trying to find a good way to handle multicast group members.Keep track
I'm trying to find a way to keep track of files even when they
I'm not a JS-guy but I am trying to find a way to make
Trying to find a way to send a POST HTTPS request from Python to

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.