Twitter OAuth 无法检索访问令牌
Twitter OAuth unable to retrieve the access token
我正在 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":"Invalid / expired
Token","request":"/oauth/access_token"}' with HTTP response status
401 instead of returning the authentication values.
事实证明,我的特定问题是由会话和回调引起的 url。
我通过 localhost/path/to/file 访问索引页面,但 Twitter 被重定向到 127.0.0.1/path/to/file 用户身份验证后,意味着存储在 localhost 上的会话数据无法在 127.0.0.1.
上访问
使用 127.0.0.1 而不是 localhost 访问索引页面解决了问题。
我正在 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":"Invalid / expired Token","request":"/oauth/access_token"}' with HTTP response status 401 instead of returning the authentication values.
事实证明,我的特定问题是由会话和回调引起的 url。
我通过 localhost/path/to/file 访问索引页面,但 Twitter 被重定向到 127.0.0.1/path/to/file 用户身份验证后,意味着存储在 localhost 上的会话数据无法在 127.0.0.1.
上访问使用 127.0.0.1 而不是 localhost 访问索引页面解决了问题。