twitteroauth icon indicating copy to clipboard operation
twitteroauth copied to clipboard

Unable to authenticate using PHP/OAuth for Twitter

Open sainathk21 opened this issue 13 years ago • 10 comments

Hello, I am trying to develop a site where users can log in, and post tweets in their accounts. I have registered the app, got the keys, etc. My problem is in the code.

I have 4 files : Config.php, connect.php, redirect.php.

Flow is like this - User opens Connect.php -> Is redirected to twitter for sign in -> Callback is redirect.php -> User enters tweet, sends -> Tweet is posted.

Unfortunately, I am facing a problem after the callback. The error I am getting is -


Notice: Undefined index: oauth_token in C:\Program Files (x86)\xampp\htdocs\SpartanTweets\redirect.php on line 6 Notice: Undefined index: oauth_token_secret in C:\Program Files (x86)\xampp\htdocs\SpartanTweets\redirect.php on line 7 Notice: Undefined index: oauth_token in C:\Program Files (x86)\xampp\htdocs\SpartanTweets\twitteroauth.php on line 114 Notice: Undefined index: oauth_token_secret in C:\Program Files (x86)\xampp\htdocs\SpartanTweets\twitteroauth.php on line 114 Notice: Undefined index: oauth_token in C:\Program Files (x86)\xampp\htdocs\SpartanTweets\redirect.php on line 11 Notice: Undefined index: oauth_token_secret in C:\Program Files (x86)\xampp\htdocs\SpartanTweets\redirect.php on line 11


Am I missing something here? This is my code for redirect.php -

getAccessToken($_REQUEST['oauth_verifier']); $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token_credentials ['oauth_token'], $token_credentials['oauth_token_secret']); // Set status message $tweetMessage = 'Twitter :D'; // Check for 140 characters if(strlen($tweetMessage) <= 140) { // Post the status message $connection->post('statuses/update', array('status' => $tweetMessage)); echo "Tweet Posted"; } ?>

Thanks! Sai

sainathk21 avatar Jul 08 '13 17:07 sainathk21

For the sake of Completeness, This is my code for connect.php -

getRequestToken(OAUTH_CALLBACK); $_SESSION['oauth_token'] = $token = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; $redirect_url = $tweet->getAuthorizeURL($request_token); header('Location: ' . $redirect_url); ?>

sainathk21 avatar Jul 08 '13 17:07 sainathk21

When a user returns from twitter.com they should land on callback.php, not redirect.php.

E.g. connect.php => redirect.php => api.twitter.com => callback.php => index.php

abraham avatar Jul 08 '13 17:07 abraham

The problem is that after a user returns from api.twitter.com you have to exchange the request token for an access token as demoed in callback.php.

abraham avatar Jul 08 '13 17:07 abraham

I'll try that right now, thank you for the fast response!

sainathk21 avatar Jul 08 '13 17:07 sainathk21

Okay, I did it this way now -

Connect.php -> Redirect.php -> tweet.php (Same as index.php). Now the twitter redirection page takes a loooong time, and then I get this error -

The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This is the relevant code from redirect.php (I have not added your test statements yet, just want to establish basic functionality first)

getAccessToken($_REQUEST['oauth_verifier']); $_SESSION['access_token'] = $access_token; unset($_SESSION['oauth_token']); unset($_SESSION['oauth_token_secret']); header('Location: ./tweet.php'); ?>

sainathk21 avatar Jul 08 '13 17:07 sainathk21

One important thing, My connect.php also has the redirect part of the code which redirect.php has in your demo.

i.e : Connect.php : Requests tokens and redirects user to api.twitter.com Redirect.php : (Wrongly named) Receives user after twitter visit, and exchanges tokens. Redirects to tweet.php Tweet.php : Sends a tweet (Same as Index.php in your demo)

Sorry for the confusion!

sainathk21 avatar Jul 08 '13 17:07 sainathk21

It looks like you are still not using callback.php.

abraham avatar Jul 08 '13 17:07 abraham

Ah as I mentioned earlier, I wrongly named Callback.php as Redirect.php :) (It contains all the code from callback.php except for the check to see if oauth_token is old).

Wait let me make this much simpler by renaming my files as per your demo!

sainathk21 avatar Jul 08 '13 17:07 sainathk21

Okay file structure is now this -

  1. Connect.php (Also contains code for Redirect.php, I am trying to keep them both is the same page)
  2. Callback.php
  3. Index.php

So the flow would be -> connect.php => api.twitter.com => callback.php => index.php

Here the issue is that it does not direct properly to index.php, giving me the error that I mentioned 3 comments ago.

Something interesting that I noticed this time was that the URL in the browser after I got the error was this -

"http://127.0.0.1/SpartanTweets/callback.php/clearsessions.php"

Relevant code -

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'); } ?>

sainathk21 avatar Jul 08 '13 18:07 sainathk21

Managed to get it to work, Thank you! (I have a slightly different question which I will ask in a separate discussion)

sainathk21 avatar Jul 08 '13 20:07 sainathk21