1

I saw previous similar threads but none of them solved my issue.
I have put button on my site and want to perform user twitter login there.

When I execute php script directly it gives expected result, but while calling it from html button click, it gives "302 found error" in firebug console.

What is wrong here?

I want to get echo json_encode((object) array('id' => $id, 'name' => $name)); in login.php as a json result.

HTML/JS code

<input type = "button" id = "loginTwitter" class = "btn btn-primary"  value = "Login | Twitter "/>
function getTwitterVal(clb)
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
    xmlhttp.onreadystatechange=function()
    {
                var result = xmlhttp.responseText;
                        alert(result);
//                var obj = JSON.parse(result);
                clb(obj);
    }
    xmlhttp.open("POST","p3.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send();
    // api_call();
}
$('#loginTwitter').click(function data() {
    getTwitterVal(function(obj) {
//        alert(obj.name);
    });
});

p3.php

<?php
/* Load required lib files. */
        session_start();
        require_once('twitteroauth/twitteroauth.php');
        require_once('config.php');
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
        $request_token = $connection->getRequestToken(OAUTH_CALLBACK);
        $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
        $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
        switch ($connection->http_code)
        {
        case 200:
                $url = $connection->getAuthorizeURL($token);
                header('Location: ' . $url);
                break;
        default:
                echo 'Could not connect to Twitter. Refresh the page or try again later.';
        }
?>

callback.php

<?php
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
  $_SESSION['oauth_status'] = 'oldtoken';
  header('Location: ./clearsessions.php');
}
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);     
$_SESSION['access_token'] = $access_token;     
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
if (200 == $connection->http_code) {
  $_SESSION['status'] = 'verified';
  header('Location: ./index.php');
} else {
  header('Location: ./clearsessions.php');
}

index.php

 <?php
            session_start();
            require_once('twitteroauth/twitteroauth.php');
           require_once('config.php');
            if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
                header('Location: ./clearsessions.php');
            }
            $access_token = $_SESSION['access_token'];
            $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
            $content = $connection->get('account/verify_credentials');
           $id = $content->{'id'};
           $name = $content->{'name'};
            echo json_encode((object) array('id' => $id, 'name' => $name));
    ?>
  • @ShankarDamodaran: please see that does not solve my issue. Here I have explained more .. – Programming_crazy Jan 31 '14 at 6:51
  • Seems like the p3.php is sending a 302 http status code. – Shankar Damodaran Jan 31 '14 at 6:54
  • @ShankarDamodaran: thanks, I am not sure. I appreciate if you please explore little more .. – Programming_crazy Jan 31 '14 at 7:28
2

twitterLoginForSite

Add twitter login button on your site and get user profile data into db

There are couple of files to do this in nice flow

Get source here : GitHub link

  1. config.php - Add you twitter auth keys on this page. Later we can access those value just by including this file

  2. login.php - You site page. We will keep login button here

  3. redirect.php - This page will start session and load library function. It creates connection using your twitter keys and redirect to twitter authentication system. Here user authenticates your Twitter apps and enters his twitter credentials

  4. callback.php - After user enters correct parameters he is redirect to this page. If the oauth_token is old redirect to the connect page. If the user has been verified then the access tokens can be saved for future use. it will do it automatically. Here you can now get user profile info. But for easiness we will do it in next page index.php

  5. index.php - Get user profile info and store it in db

Customization of Abrahams twitter auth github source.

  • The problem with this is that in the Git link both "callback.php" and "redirect.php" have a line of code:require_once('twitteroauth/twitteroauth.php'), and that php file is not included. How and where do we get this? – bagofmilk Jul 23 '14 at 19:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.