Twitter login on button click using abraham's oauth needs help


#1

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);
                echo "hi";
                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));

#2

@abraham I appreciate if you give piece of time …


#3

header('Location: ’ . $url); tells the browser to perform a redirect so you can’t call it through XHR and expect the users window to redirect. Either change p3.php to return the url as a response that JavaScript redirects to or use the link to send the user to p3.php directly.


#4

اشكر لك اهتمامك
لاعدمتك


#5

; tells the browser to perform a redirect so you can’t call it through XHR and expect the users window to redirect. Either change p3.php to return the url as a response that JavaScript redirects to or use the link to send the user to p3.php directly