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

  • SEARCH
  • Home
  • 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 9258115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:18:50+00:00 2026-06-18T12:18:50+00:00

I am using Twitter to log users into to a website, which seems to

  • 0

I am using Twitter to log users into to a website, which seems to be working up until I attempt to obtain a valid Access Token.

require("twitteroauth.php");
require 'twconfig.php';
session_start();

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
$request_token = $twitteroauth->getRequestToken('http://****/tw_response.php');

$oauth_token = $request_token['oauth_token'];
$_SESSION['oauth_token'] = $oauth_token;

$oauth_token_secret = $request_token['oauth_token_secret'];
$_SESSION['oauth_token_secret'] = $oauth_token_secret;

if ($twitteroauth->http_code == 200) {
    url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
    header('Location: '.$url);
} else {
    die('Something wrong happened.');
}

This seems to be working correctly, redirecting me to twitter to sign in and confirm access, after which it returns me to tw_response.php (my Callback url), with the following variables in the url:

http://example.com/login.php?oauth_token=sO3X...yj0k&oauth_verifier=Ip6T...gALQ 

In tw_response.php I then try to get the Access Token, but it reports as invalid. I tried using var_dump to view the content of the access token as follows:

require("twitteroauth.php");
require 'twconfig.php';
session_start();

$oauth_verifier = $_REQUEST['oauth_verifier'];
$oauth_token = $_SESSION['oauth_token'];
$oauth_token_secret = $_SESSION['oauth_token_secret'];

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $oauth_token, $oauth_token_secret);

$access_token = $twitteroauth->getAccessToken($data['oauth_verifier']);
var_dump($access_token);

The result of the var_dump ends in “invalid / expired Token”:

array(8) {
    ["oauth_url"] => string(104) ""1.0" encoding="UTF-8"?>/oauth/access_token?oauth_consumer_key=ceE...9Dg"
    ["oauth_nonce"]=> string(32) "c52...d07"
    ["oauth_signature"]=> string(28) "ry7...Fcc="
    ["oauth_signature_method"]=> string(9) "HMAC-SHA1"
    ["oauth_timestamp"]=> string(10) "1359031586"
    ["oauth_token"]=> string(40) "sO3...j0k"
    ["oauth_verifier"]=> string(43) "Ip6...ALQ"
    ["oauth_version"]=> string(63) "1.0 Invalid / expired Token "
}
  • 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-06-18T12:18:52+00:00Added an answer on June 18, 2026 at 12:18 pm
    $access_token = $twitteroauth->getAccessToken($data['oauth_verifier']);
    var_dump($access_token);
    

    Where did $data magically come from? You have the variable $oauth_verifier, but keep in mind you don’t need this if this is your registered callback URL.

    Since you used an invalid variable inside getAccessToken, it will return an invalid value back.

    The correct way to use TwitterOAuth:

    if (!isset($_GET["oauth_token"])) {
        // set these values in a config file somewhere.
        $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    
        // append a ?. This is your callback URL if you specify something.
        $credentials = $twitter->getRequestToken("http://example.com/test.php?");
    
        // try and be a bit more elegant with the URL... This is a minimal example
        $url = $twitter->getAuthorizeUrl($credentials);
        echo $url;
    
        // these are temporary tokens that must be used to fetch the new,
        // permanent access tokens. store these in some way,
        // session is a decent choice.
        $_SESSION["token"] = $credentials["oauth_token"];
        $_SESSION["secret"] = $credentials["oauth_token_secret"];
    } else {
    
        // use the user's previously stored temporary credentials here
        $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,
                        $_SESSION["token"], $_SESSION["secret"]);
    
        // uses the oauth_token (from the request) already.
        // you store these credentials in your database (see below).
        $credentials = $twitter->getAccessToken($_GET["oauth_verifier"]);
    
        // just a printout of credentials. store these, don't display them.
        echo "<pre>";
        var_dump($credentials);
        // valid credentials, provided you give the app access to them.
        echo "</pre>";
    }
    

    I just use a single script for callbacks for ease of use; you can split the relevant sections into multiple scripts if you like (and you probably should).

    Handily for your database, the credentials include the twitter user’s username, too.
    Edit: Twitter is now allocating 64bit integers for user IDs. You should store this as a string to ensure that you don’t end up with mangled user IDs and collisions if you can’t handle 64bit integers in every part of your application.

    array(4) {
      ["oauth_token"]=>
      string(50) "7041...wYupkS"
      ["oauth_token_secret"]=>
      string(42) "O9ENq...21B2fk"
      ["user_id"]=> // user ID. always the same, never changes (store this as ID)
      string(9) "..."
      ["screen_name"]=> // username. can change.
      string(11) "..."
    }
    

    So, if you want to log users in through twitter, without explicitly giving them a login to your site, you could use $_SESSION (I use databases for my logins, which is recommended if you want to save that state)
    In the above script you would add this to the end of the else block:

    $_SESSION["token"] = $credentials["oauth_token"];
    $_SESSION["secret"] = $credentials["oauth_secret"];
    $_SESSION["username"] = $credentials["screen_name"];
    

    You can also get the user’s screen name and more from GET account/verify_credentials, if you want to give them a user page (if you use javascript, grab their userid through id_str here):

    $user_array = $twitter->get("account/verify_credentials");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using tweetsharp to allow users to log into my site with their twitter
I developed a site using twitter bootstrap and it seems that the responsive layout
I'm using OAuth in my web app, and users can login with twitter. I
I have an App that allows users to log in with their twitter, facebook,
I want to allow my users to log in with their Twitter accounts. What
I'm using mechanize ( and following this tutorial ) to try to log into
Does anyone know how to log a user out of Twitter using Tweetsharp? I
I making a php server/page which is supposed to capture a users twitter feed
Using Twitter Bootstrap's bootstrap-tab.js, I have: <ul class=tabnavcenter id=myTab> <li class=active><a href=#home data-toggle=tab>about</a></li> <li><a
Using Twitter Bootstrap's [Tabbable Tabs][1], it says to: Enable tabbable tabs via javascript (each

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.