dpvmtdu364462 2018-05-11 23:19
浏览 243
已采纳

Twitter API和获取用户信息PHP

I'm having issues with Twitter API and understanding OAuth in general. I'm able to make request to pull information from my account with ease. The problem I'm having is with other users who would be using "Sign In with Twitter". Even though I am able to get other user information after they sign in, I'm unable to make separate future request with their information on other .php pages (I am not trying to pull info from MySQL). I can only get their information one time on the original .php page after they sign in and the page has loaded.

I will post some code but my main concerns/questions are -- is it possible to save user access token information (and re-use) or will I be needing to have the user sign in every time and authenticate just to pull information from their account? I am having trouble understanding this. What information can I save to make a request in the future on behalf of a user with out having to have them log in every time?

Code example:

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']));

}
  • 写回答

1条回答 默认 最新

  • douliexing2195 2018-05-16 22:38
    关注

    What you need in order to maintain access to user information on behalf of them is the generated oAuth Token and oAuth Token Secret. In my particular case listed above, the steps should be

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

    You will need your own application CONSUMER_KEY and CONSUMER_SECRET. When someone signs in with Twitter, you have to save their oAuth Token and oAuth Token Secret. When you have this information (stored in a database), you can now make calls on behalf of the user for future request.

    More into the specific problem I had listed above, I was not saving this information. I kept making new oAuth Tokens and Secrets.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?