0

I can't seem to save the Twitter user_id, screen_name, oauth_token and oauth_token_secret (from a logged in user) into a mysql database that I've set up?

I'm using the Abraham Williams Oauth library.

That code works fine and I can see the components that make up the access_token by using a print_r request when a user logs in, however the tokens aren't saved into the table 'users' in the database 'tokens'?

I've read nearly all the questions/answers on SO and tested every bit of code however I can't seem to get a simple INSERT to work for these tokens? I've also hard coded some test components into the config_db file (as an INSERT) and they load fine.

Callback code:

<?php

require_once("/path/config_db.php");

session_start();
// Include class & create
require_once('/path/config.php');
require_once('/path/twitteroauth/twitteroauth.php');
// User has selected to DENY access
if(!empty($_GET["denied"])) {
  // could re-direct or display cancelled view/template
  // we're just echoing out a message
  echo "No deal! <a href='index.php'>Try again?</a>";
  die();
}

/* If the oauth_token is old redirect to the connect page. */
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
  $_SESSION['oauth_status'] = 'oldtoken';
  header('Location: ./clearsessions.php');
}

/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

/* Request access tokens from twitter */
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);

//echo "RECEIVED TOKENS<br>";
// Check we have valid response
if(is_numeric($access_token["user_id"]))  {
// Save the access tokens to a DB (we're using a session)
/* Save the access tokens. Normally these would be saved in a database for future use. */
$_SESSION['access_token'] = $access_token;

//GET CREDENTIALS VIA API
$credentials = $connection->get('account/verify_credentials');

//insert tokens into db
print_r($_SESSION["access_token"]);
$sql="INSERT INTO users (`user_id` ,`screen_name` ,`oauth_token` ,`oauth_token_secret`) 
VALUES ('".$_SESSION["access_token"]["user_id"]."', 
'".$_SESSION["access_token"]["screen_name"]."', 
'".$_SESSION["access_token"]["oauth_token"]."', 
'".$_SESSION["access_token"]["oauth_token_secret"]."'";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

}
//echo $query;
//echo mysql_error();
print_r($_SESSION["access_token"]);
$message = array('status' => 'Test OAuth update. #testoauth');
$test = $connection->post('statuses/update', array('status' => 'Just a test '));

/* Remove no longer needed request tokens */
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);

/* If HTTP response is 200 continue otherwise send to connect page to retry */
if (200 == $connection->http_code) {
  /* The user has been verified and the access tokens can be saved for future use */
  $_SESSION['status'] = 'verified';
  header('Location: ./callback.php');
} else {
  /* Save HTTP status for error dialog on connnect page.*/
  header('Location: ./clearsessions.php');
}

?>

<? print_r($access_token); ?>

The connection (config_db file) is as follows

<?php

$con=mysqli_connect("server","username","password","tokens");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_close($con);

?>

The table 'users' is as follows:

$sql = "CREATE TABLE users 
(
user_id INT(11),
screen_name varchar(50),
oauth_token varchar(90),
oauth_token_secret varchar(90),
)";
Dharman
  • 30,962
  • 25
  • 85
  • 135
Tatters
  • 1,187
  • 2
  • 9
  • 18
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Dec 30 '19 at 14:38

1 Answers1

0

You have a syntax error on sql query. you forget the close parentheses.

'".$_SESSION["access_token"]["oauth_token_secret"]."'";  change this 
'".$_SESSION["access_token"]["oauth_token_secret"]."')";
shx2
  • 61,779
  • 13
  • 130
  • 153