【问题标题】:Twitter API and obtaining user information PHPTwitter API 和获取用户信息 PHP
【发布时间】: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']));

}

【问题讨论】:

    标签: php api twitter oauth


    【解决方案1】:

    为了代表他们维护对用户信息的访问,您需要的是生成的 oAuth Token 和 oAuth Token Secret。在我上面列出的特定情况下,步骤应该是

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

    您将需要自己的应用程序 CONSUMER_KEY 和 CONSUMER_SECRET。当有人使用 Twitter 登录时,您必须保存他们的 oAuth Token 和 oAuth Token Secret。当您拥有此信息(存储在数据库中)后,您现在可以代表用户拨打电话以备将来请求。

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

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 2015-11-12
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 2021-11-23
      • 2013-08-19
      • 2021-10-12
      • 2022-01-11
      相关资源
      最近更新 更多