简体   繁体   English

将 Twitter 令牌存储在 mysql 数据库中

[英]Store Twitter Tokens in mysql database

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?我似乎无法将 Twitter user_id、screen_name、oauth_token 和 oauth_token_secret(来自登录用户)保存到我设置的 mysql 数据库中?

I'm using the Abraham Williams Oauth library.我正在使用 Abraham Williams Oauth 库。

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'?该代码运行良好,我可以在用户登录时使用 print_r 请求查看组成 access_token 的组件,但是令牌未保存到数据库“令牌”中的“用户”表中?

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?我已经阅读了几乎所有关于 SO 的问题/答案并测试了每一位代码,但是我似乎无法获得一个简单的 INSERT 来处理这些令牌? I've also hard coded some test components into the config_db file (as an INSERT) and they load fine.我还将一些测试组件硬编码到 config_db 文件中(作为 INSERT)并且它们加载良好。

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连接(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);

?>

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),
)";

You have a syntax error on sql query.您在 sql 查询上有语法错误。 you forget the close parentheses.你忘记了右括号。

'".$_SESSION["access_token"]["oauth_token_secret"]."'";  change this 
'".$_SESSION["access_token"]["oauth_token_secret"]."')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM