【发布时间】:2018-10-22 09:47:15
【问题描述】:
我在使用 Twitter API 和理解 OAuth 时遇到了问题。我可以轻松地请求从我的帐户中提取信息。我遇到的问题是其他使用“使用 Twitter 登录”的用户。即使我能够在他们登录后获取其他用户信息,但我无法在其他 .php 页面上使用他们的信息提出单独的未来请求(我不是试图从 MySQL 中提取信息)。在他们登录并加载页面后,我只能在原始 .php 页面上获取他们的信息一次。
我将发布一些代码,但我的主要顾虑/问题是——是否可以保存用户访问令牌信息(并重复使用)或者我是否需要让用户登录每次都进行身份验证只是为了从他们的帐户中提取信息?我很难理解这一点。我可以保存哪些信息以便将来代表用户提出请求,而不必让他们每次都登录?
代码示例:
require "autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', 'my consumer key');
define('CONSUMER_SECRET', 'secret');
define('OAUTH_CALLBACK', 'API/Twitter/Twitter.php');
$access_token = 'beep boop boop beep';
$access_token_secret = 'super secret';
session_start();
if (isset($_SESSION['oauth_token'])) {
$oauth_token = $_SESSION['oauth_token'];
echo "<div style='background-color:white; width:100%;'>";
echo $oauth_token; echo "</div>";
unset($_SESSION['oauth_token']);
$connection = new Abraham\TwitterOAuth\TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$params = array("oauth_verifier" => $_GET['oauth_verifier'], 'oauth_token' => $_GET['oauth_token']);
$access_token = $connection->oauth('oauth/access_token', $params);
$connection = new Abraham\TwitterOAuth\TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->get('account/verify_credentials');
//Printing the profile data
//print_r($content);
$TimeLine = $connection->get("statuses/user_timeline", ["screen_name"=>$content->screen_name, "count"=>10]);
echo "<br><br><br>";
echo "<div style='width:100%; background-color:red; height:auto;'>";
print_r($connection);
echo "</div>";
//print_r($TimeLine);
} else {
$connection = new Abraham\TwitterOAuth\TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$temporary_credentials = $connection->oauth('oauth/request_token', array("oauth_callback" => $callback));
$_SESSION['oauth_token'] = $temporary_credentials['oauth_token'];
$_SESSION['oauth_token_secret'] = $temporary_credentials['oauth_token_secret'];
$url = $connection->url('oauth/authenticate', array('oauth_token' => $temporary_credentials['oauth_token']));
}
【问题讨论】: