加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:48:17 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试编写几个php页面来获取Twitter API 1.1的用户令牌.我正在使用TwitterOAuth库 https://twitteroauth.com/ 第一页:twitter-go.php 用户打开它并被重定向到twitter.com以授权该应用程序. 我猜这是使用POST oauth / request_token和GET oauth / autho
我正在尝试编写几个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函数的地方.

这是获取用户秘密令牌和访问令牌的正确方法吗?

万分感谢!

亚瑟

解决方法

好吧,实际上我自己弄明白了.以下是我需要它的代码:

第一页:twitter-go.php

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

<?php

//LOADING LIBRARY
require "twitteroauth/autoloader.php";
use AbrahamTwitterOAuthTwitterOAuth;

//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

一旦授权应用程序,用户就会从Twitter重定向到那里.然后它显示用户访问令牌和用户访问密钥.

<?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 AbrahamTwitterOAuthTwitterOAuth;

//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,$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/

希望有所帮助;)

亚瑟

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读