推特API和获取用户信息PHP



我在 Twitter API 和一般理解 OAuth 方面遇到了问题。我可以轻松地请求从我的帐户中提取信息。我遇到的问题是其他用户将使用"使用Twitter登录"。即使我能够在其他用户登录后获取他们信息,我也无法在其他.php页面上使用他们的信息提出单独的未来请求(我不是在尝试从 MySQL 中提取信息(。在他们登录并加载页面后,我只能在原始.php页面上获取一次他们的信息。

我将发布一些代码,但我的主要关注点/问题是- 是否可以保存用户访问令牌信息(并重复使用(,或者我是否需要每次都让用户登录并进行身份验证只是为了从他们的帐户中提取信息?我很难理解这一点。我可以保存哪些信息,以便将来代表用户发出请求,而不必让他们每次都登录?

代码示例:

require "autoload.php";
use AbrahamTwitterOAuthTwitterOAuth;
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 AbrahamTwitterOAuthTwitterOAuth(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 AbrahamTwitterOAuthTwitterOAuth(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 AbrahamTwitterOAuthTwitterOAuth(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']));
}

为了代表用户保持对用户信息的访问,您需要的是生成的oAuth 令牌和 oAuth 令牌密钥。在上面列出的特定情况下,步骤应该是

$connection = new AbrahamTwitterOAuthTwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, USER_oAuth_TOKEN, USER_oAuth_TOKEN_SECRET);
$content = $connection->get('account/verify_credentials');

您将需要自己的应用程序CONSUMER_KEY和CONSUMER_SECRET。当有人使用 Twitter 登录时,您必须保存他们的 oAuth 令牌和 oAuth 令牌密钥。当您拥有此信息(存储在数据库中(时,您现在可以代表用户进行调用以备将来请求。

更深入到我上面列出的具体问题,我没有保存这些信息。我一直在制作新的oAuth令牌和秘密。

最新更新