Jump to content

PHP to Twitter OAuth


Recommended Posts

Good day Campers!

 

Having spent the better part of my day trawling the internet for an answer I have gotten nowhere so have come to you lovely people for help.

 

I found a nice little tutorial on PHP gang that would allow me to update my twitter feed from my website using PHP and OAuth.

I set up my twitter app with no issues and generated the API keys. Set the App to 'read/write' permissions and then regenerated the keys

The app is marked to "Allow this application to be used to Sign in with Twitter"

 

The issue is that when I click the little button to sign in to twitter, the page redirects but displays the generic "Cannot connect to Twitter" error message that has been defined in the code. I do not get asked to "Allow" the script to connect to twitter like it seems to do in the demo.

 

The PHPGang solution has 4 bits of script so I'm wondering if it has something to do with this as I understand the Abrahams script has slightly more so I do wonder if something is missing. I'm not sure. One of the main problems I am facing is that I have never actually done this before so I don't know where I have gone wrong or what I am even looking for help wise......

 

I have the following scripts:

callback.php

<?php
/**
 * Take the user when they return from Twitter. Get access tokens.
 * Verify credentials and redirect to based on response from Twitter.
 */
session_start();
require_once('oauth/twitteroauth.php');
require_once('config.php');

if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
  $_SESSION['oauth_status'] = 'oldtoken';
  header('Location: ./destroysessions.php');
}

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
//save new access tocken array in session
$_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: ./destroysessions.php');
}
?>

config.php

<?php
/**
 * @file
 * A single location to store configuration.
 */
define('CONSUMER_KEY', 'MY CONSUMER KEY');
define('CONSUMER_SECRET', 'MY SECRECT CONSUMER KEY');
define('OAUTH_CALLBACK', 'URL OF WHERE I AM REDIRECTING TO');
?>

destroysessions.php

<?php
/**
 * @file
 * Clears PHP sessions and redirects to the connect page.
 */

/* Load and clear sessions */
session_start();
session_destroy();

/* Redirect to page with the connect to Twitter option. */
header('Location: ../index.php');
?>

index.php

	<?php
	require_once('oauth/twitteroauth.php');
	require_once('config.php');
		if(isset($_POST["status"]))
			{
				$status = $_POST["status"];
				if(strlen($status)>=130)
				{
					$status = substr($status,0,130);
				}
				$access_token = $_SESSION['access_token'];
				$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

				$connection->post('statuses/update', array('status' => "$status"));
				$message = "Tweeted Sucessfully!!";
			}
		if(isset($_GET["redirect"]))
			{
				$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 '<div class="par">Could not connect to Twitter. Refresh the page or try again later.</div>';
				}
		exit;
			}
		if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {

				echo '<a href="./index.php?redirect=true"><img src="./images/lighter.png" alt="Sign in with Twitter"/></a>';
			}
		else
			{
				echo "<a href='destroysessions.php'>Logout</a><br>";
				echo '<div class="par">'.$message.'<div>
				<form action="index.php" method="post">
				    <input type="text" name="status" id="status" placeholder="Write a comment....">
				    <input type="submit" value="Post On My Wall!" style="padding: 5px;">
				</form>';
			}
	?>

Now, I am unsure if there is anything wrong with the API keys I have generated or if there is something missing.

Is there anyone who can point me in the right direction or offer some advice on where to turn?

 

Thanks!

Edited by lauren_etherington
Link to comment
Share on other sites

Okay so I have attempted to use Abrahams OAuth plugin.

I have added in my Consumer and Secret Key however I have received the precoded message  "You need a consumer key and secret to test the sample code. Get one from dev.twitter.com/apps"

Obviously this looks like there is something up the the keys,

Is there anyway to trouble shoot as to why the keys are not "active"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.