推特 OAuth 1.0。 Opera 重定向失败
Posted
技术标签:
【中文标题】推特 OAuth 1.0。 Opera 重定向失败【英文标题】:Twitter OAuth 1.0. Opera redirection failure 【发布时间】:2011-12-16 21:46:18 【问题描述】:我目前正在网站上工作。将有几种类型的授权,其中一种是“通过 Twitter”。我正在使用 Abraham Williams 的 TwitterOAuth 库。我将尝试解释以下脚本的工作原理:
-
当您点击 index.php 上的“登录”按钮时,弹出窗口 (login.php) 会打开并要求授权该应用程序使用您的 Twitter 帐户
提交后,将重定向到 api.twitter.com,并在 $_GET 参数中使用“访问令牌”。
然后 twitter 授权该“令牌”,并返回到回调 URL,该 URL 在 twitter 应用程序的设置中定义。
它工作得很好,除了从 api.twitter.com 重定向到回调 URL 时,opera 有问题。似乎根本没有执行重定向。
以下是来源:
index.php
<?php
session_start();
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
function popup(url)
var addr = document.location.href.replace('index.php','');
popUpObj = window.open(addr + url, 'Twitter', 'width=700,height=600,menubar=yes,status=yes');
function transferdata(data)
if (data.screen_name)
(elem = document.getElementById('tw-login')).parentNode.removeChild(elem);
document.getElementById('menu').innerHTML = 'Hello, ' + data.screen_name;
else
document.getElementById('menu').innerHTML = 'Twitter didn\'t authorize you :(';
</script>
<style type="text/css">
#tw-login
display: block;
background-color: #ccc;
text-align: center;
font-family: "Trebuchet MS", Vernanda, serif;
color: #fff;
margin: 5px;
padding: 4px 6px;
text-decoration: none;
width: 80px;
#tw-logout
display: block;
background-color: #ccc;
text-align: center;
font-family: "Trebuchet MS", Vernanda, serif;
color: #fff;
margin: 5px;
padding: 4px 6px;
text-decoration: none;
width: 80px;
</style>
</head>
<body>
<a id="tw-login" href="" onclick="popup('login.php');">Log in</a>
<a id="tw-logout" href="session_clear.php">Log out</a>
<div id="menu">
</div>
</body>
</html>
login.php
<?php
session_start();
include 'twitteroauth/twitteroauth.php';
define('TWITTER_KEY', '*******');
define('TWITTER_KEY_SECRET', '*******');
$twitteroauth = new TwitterOAuth(TWITTER_KEY, TWITTER_KEY_SECRET);
$requestToken = $twitteroauth->getRequestToken();
$_SESSION['oauth_token'] = $requestToken['oauth_token'];
$_SESSION['oauth_token_secret'] = $requestToken['oauth_token_secret'];
if($twitteroauth->http_code == 200)
$url = $twitteroauth->getAuthorizeURL($requestToken['oauth_token']);
header('Location: ' . $url);
else
die('Something wrong happened.');
callback.php
<?php
session_start();
include 'twitteroauth/twitteroauth.php';
define('TWITTER_KEY', '*******');
define('TWITTER_KEY_SECRET', '*******');
if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret']))
$twitteroauth = new TwitterOAuth(TWITTER_KEY, TWITTER_KEY_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$accessToken = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
$_SESSION['access_token'] = $accessToken;
$userinfo = $twitteroauth->get('account/verify_credentials');
else
header('Location: login.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(SendToMain());
function SendToMain()
if(window.opener != null || !window.opener.closed)
window.opener.transferdata(<?php echo json_encode($userinfo); ?>);
window.close();
</script>
</head>
<body>
</body>
</html>
session_clear.php
<?php
session_start();
session_destroy();
header('Location: index.php');
【问题讨论】:
如果这些 API 密钥真的是秘密的,也许你应该从公开的 *** 帖子中删除它们 :-) 【参考方案1】:您的重定向 URI 不使用 ssl。要解决此问题,请改用 https。
【讨论】:
【参考方案2】:您必须先向某些浏览器发送 301 或 303 之类的状态代码,然后它们才会重定向。试试这样的:
header("Status: 303");
header("Location: /home.php");
exit;
【讨论】:
我的重定向工作正常。从 api.twitter.com 重定向到回调失败。不幸的是,我无法访问 twitter.com 资源 :) 在这种情况下,您必须提供更多详细信息 - 因为在不知道 Twitter 实际如何执行重定向的情况下,我们无法对此进行调试 :-( 如果您不想给我们提供的 URL显示问题,也许您可以使用 Fiddler HTTP 调试器来探索 Twitter 发送到各种浏览器的内容?顺便说一句,您要重定向到的站点是在内网还是互联网上?以上是关于推特 OAuth 1.0。 Opera 重定向失败的主要内容,如果未能解决你的问题,请参考以下文章
Facebook 登录消息:“URL 被阻止:此重定向失败,因为重定向 URI 未在应用程序的客户端 OAuth 设置中列入白名单。”
“URL 被阻止:此重定向失败,因为重定向 URI 未在应用程序的客户端 OAuth 设置中列入白名单。”但在本地工作
Facebook 登录消息:“URL 被阻止:此重定向失败,因为重定向 URI 未在应用程序的客户端 OAuth 设置中列入白名单。”