【发布时间】:2015-05-28 11:09:28
【问题描述】:
我正在用 PHP 构建一个 Twitter 应用程序,使用 TwitterOAuth 库进行用户身份验证。
我能够将用户重定向到 Twitter 进行身份验证并接收 oauth 令牌、oauth 密码和 oauth 验证程序,但是我无法完成获取访问令牌的身份验证的最后一步。 我正在本地主机上开发,我已经设置了回调路径
http://127.0.0.1:80/TwitterApp/update.php
我的应用具有读写权限。
这是我的代码:
index.php
<?php
session_start();
include "twitteroauth/autoload.php";
include "oauthConsts.php";
use Abraham\TwitterOAuth\TwitterOAuth;
// request authentication tokens
$oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $oauth->getRequestToken(
'http://127.0.0.1/TwitterApp/update.php');
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_secret'] = $request_token['oauth_token_secret'];
if($oauth->getLastHttpCode()==200){
// Let's generate the URL and redirect
$url = $oauth->getAuthorizeURL($request_token['oauth_token']);
header('Location: '. $url);
} else {
echo "something went wrong";
}
?>
update.php
<?php
session_start();
include "twitteroauth/autoload.php";
include "oauthConsts.php";
use Abraham\TwitterOAuth\TwitterOAuth;
if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token'])
&& !empty($_SESSION['oauth_secret'])) {
$oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,
$_SESSION['oauth_token'], $_SESSION['oauth_secret']);
$access_token = $oauth->oauth("oauth/access_token",
array("oauth_verifier" => $_GET['oauth_verifier']));
$_SESSION['access_token'] = $access_token;
}
else {
header('Location: index.php');
}
?>
执行时,update.php 中的 $access_token 变为
'{"error":"无效/过期 带有 HTTP 响应状态的令牌","request":"/oauth/access_token"}' 401 而不是返回身份验证值。
【问题讨论】:
标签: php authentication twitter oauth