Implementing Sign in with Twitter


#1

Implementing Sign in with Twitter
https://dev.twitter.com/docs/auth/implementing-sign-twitter
I have version (PECL OAuth >= 0.99.1) on my hostgator server : OAuth version, default value is always “1.0”

Page.php > Sign in with Twitter button goto Page2.php

Page2.php >
Step 1: Obtaining a request token : WORKS
// send my info to get request token
$oauth = new OAuth(myconsumerkey,myconsumersecret); # my app keys
$request_token_info = $oauth>getRequestToken(“http://api.twitter.com/oauth/request_token”);
// extract the request token and secret
$oauth_token = $request_token_info[‘oauth_token’];
$oauth_token_secret = $request_token_info[‘oauth_token_secret’];

Step 2: Redirecting the user : WORKS
// now send user to twitter to sign in
// send twitter back $oauth_token
// authenticate
header(“Location: https://api.twitter.com/oauth/authenticate?oauth_token=$oauth_token”);

callback.php

// twitter redirects back here after user signs in and authorises app
// extract token and verifier
$oauth_token = $_GET[oauth_token];
$oauth_verifier = $_GET[oauth_verifier];

Step 3: Converting the request token to an access token: NOT WORKING

Code still on callback.php above

// setup oauth
$oauth = new OAuth(myconsumerkey,myconsumersecret); # my app keys
$oauth->setToken($oauth_token,$oauth_verifier); # user token and verifier
// This URL gives error 401
$request_token_info = $oauth->getRequestToken(“https://api.twitter.com/oauth/access_token”); # get 401 on this url
// BUT this URL returns tokens
$request_token_info = $oauth->getRequestToken(“https://api.twitter.com/oauth/request_token”);
// extract result
$oauth_tokenUser = trim($request_token_info[‘oauth_token’]);
$oauth_token_secretUser = trim($request_token_info[‘oauth_token_secret’]);

Every time I run this the tokens have different values so I don’t know how to recognise a user
Twitter says I should only send the oauth_verifier to obtain the tokens but I don’t know how to do that
Why don’t the oauth/access_token URL work?
Are these the right tokens for this user?
I believe I can store these keys and make calls to api on users behalf at any time?

Basically I don’t know what to do next to get the users name and id

I tried
$oauth->setToken($oauth_tokenUser,$oauth_token_secretUser);
With the following URLS
$content = $oauth->fetch(‘http://api.twitter.com/1.1/account/verify_credentials.json’); # ERROR 401
$content = $oauth->fetch(‘http://api.twitter.com/1/account/verify_credentials.json’); # ERROR 401

Is it because its not a secure connection, I tried https too and still don’t work although I don’t think my server is ssl
Do I have to set any session cookies?