如何使用Twitter API获取用户访问令牌和访问密钥

6
我正在编写几个PHP页面,以获取用户的Twitter API 1.1令牌。我正在使用TwitterOAuth库https://twitteroauth.com/
第一页:twitter-go.php 用户打开此页面并被重定向到twitter.com以授权该应用程序。
我猜这是在使用POST oauth/request_token和GET oauth/authorize功能。
第二页:twitter-back.php 一旦用户授权该应用程序,他就会从Twitter重定向到此页面。它显示用户访问令牌和用户访问密钥(或将它们存储到数据库中以供以后使用)。
我猜这是在使用POST oauth/access_token功能。
这是获取用户秘密令牌和访问令牌的正确方法吗?

当您给一个负面评价时,请添加一条评论解释原因... - Arthur
1个回答

10

好的,我成功地自己解决了。以下是我的代码,供需要的人使用:

第一页:twitter-go.php

用户打开它并被重定向到twitter.com以授权应用程序。

<?php

//LOADING LIBRARY
require "twitteroauth/autoloader.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//TWITTER APP KEYS
$consumer_key = 'yourkey';
$consumer_secret = 'yourkey';

//CONNECTION TO THE TWITTER APP TO ASK FOR A REQUEST TOKEN
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
$request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "http://boulangerie-colas.fr/twitter/twitter-back.php"));
//callback is set to where the rest of the script is

//TAKING THE OAUTH TOKEN AND THE TOKEN SECRET AND PUTTING THEM IN COOKIES (NEEDED IN THE NEXT SCRIPT)
$oauth_token=$request_token['oauth_token'];
$token_secret=$request_token['oauth_token_secret'];
setcookie("token_secret", " ", time()-3600);
setcookie("token_secret", $token_secret, time()+60*10);
setcookie("oauth_token", " ", time()-3600);
setcookie("oauth_token", $oauth_token, time()+60*10);

//GETTING THE URL FOR ASKING TWITTER TO AUTHORIZE THE APP WITH THE OAUTH TOKEN
$url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));

//REDIRECTING TO THE URL
header('Location: ' . $url);

?>

第二页:twitter-back.php

一旦用户授权应用程序,他就会被重定向到此页面。页面上显示了用户访问令牌和用户访问密钥。

<?php 
/**
 * users gets redirected here from twitter (if user allowed you app)
 * you can specify this url in https://dev.twitter.com/ and in the previous script
 */ 

//LOADING LIBRARY
require "twitteroauth/autoloader.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//TWITTER APP KEYS
$consumer_key = 'yourkey';
$consumer_secret = 'yourkey';

//GETTING ALL THE TOKEN NEEDED
$oauth_verifier = $_GET['oauth_verifier'];
$token_secret = $_COOKIE['token_secret'];
$oauth_token = $_COOKIE['oauth_token'];

//EXCHANGING THE TOKENS FOR OAUTH TOKEN AND TOKEN SECRET
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $token_secret);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier));

$accessToken=$access_token['oauth_token'];
$secretToken=$access_token['oauth_token_secret'];

//DISPLAY THE TOKENS
echo "<b>Access Token : </b>".$accessToken."<br />";
echo "<b>Secret Token : </b>".$secretToken."<br />";

 ?>
请记住,您需要使用TwitterOAuth库,链接为https://twitteroauth.com/

干得好!只有一件事,在第二页中,$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier)); 中的变量名应该更改为其他名称,例如$request_access_token。 - Satys
嗨@Arthur, 希望你能帮帮我。我想获取已连接用户的数据,但是API总是返回创建应用程序的用户数据,而不是已连接的用户数据,即使我正在使用'account/verify_credentials'。请在此处查看我的问题https://stackoverflow.com/questions/46756929/twitter-api-fetch-profile-data-from-given-token-after-redirect-to-callback-url - SlimenTN
太好了,你真的救了我的一天... - Keinan Goichman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接