0

我似乎无法将 Twitter 用户 ID、屏幕名称、oauth_token 和 oauth_token_secret(来自登录用户)保存到我设置的 mysql 数据库中?

我正在使用 Abraham Williams Oauth 库。

该代码工作正常,我可以在用户登录时使用 print_r 请求查看构成 access_token 的组件,但是令牌没有保存到数据库“令牌”中的“用户”表中?

我已经阅读了几乎所有关于 SO 的问题/答案并测试了每一段代码,但是我似乎无法获得一个简单的 INSERT 来处理这些令牌?我还将一些测试组件硬编码到 config_db 文件中(作为 INSERT)并且它们加载良好。

回调代码:

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

连接(config_db文件)如下

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

?>

“用户”表如下:

$sql = "CREATE TABLE users 
(
user_id INT(11),
screen_name varchar(50),
oauth_token varchar(90),
oauth_token_secret varchar(90),
)";
4

1 回答 1

0

您对 sql 查询有语法错误。你忘记了右括号。

'".$_SESSION["access_token"]["oauth_token_secret"]."'";  change this 
'".$_SESSION["access_token"]["oauth_token_secret"]."')";
于 2013-11-16T22:06:36.557 回答