您好我正在尝试制作Twitter API。我正在使用oAuth来验证自己。问题是它没有做任何事情我得到了跟随我的指南相同。我在配置或代码中犯了错误吗?
<?php
require("src/TwitterOAuth.php");
session_start();
$twitteroauth = new TwitterOAuth("CONSUMER KEY", "CONSUMER SECRET KEY");
$request_token = $twitteroauth->getRequestToken("http://127.0.0.1:22119/twitter_login.php");
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
if($twitteroauth->http_code=200)
{
$url = $twitteroauth->getAuthorizeUrl($request_token['oauth_token']);
header('Location:'. $url);
}
else
{
die('Something wrong happened.');
}
?>
此处我的Twitter应用设置的屏幕截图:http://i.imgur.com/De2XzWU.png
以下指南:http://code.tutsplus.com/tutorials/how-to-authenticate-users-with-twitter-oauth--net-13595
提前致谢
答案 0 :(得分:1)
首先你需要在你的twitter开发者资料中添加新的应用程序,你需要指定回调网址,而不是你使用php mvc框架需要在你需要的twitter操作中转到你的控制器添加以下内容:
// TWITTER APP KEYS
$consumer_key = 'something you get from twitter dev';
$consumer_secret = 'other thing you get from twitter dev';
// 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://something/otherthing"
));
// 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'];
如果你想要,你可以在cookie中放一些变量供父亲使用:
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);
比你需要Twitter要求授权你的应用程序
// GETTING THE URL FOR ASKING TWITTER TO AUTHORIZE THE APP WITH THE OAUTH TOKEN
$url = $connection->url("oauth/authorize", array(
"oauth_token" => $oauth_token
));
并且你要做的最后一件事是渲染网址,基本上这意味着在用户批准后我们需要将他重定向到另一个页面,我们可以使用twitter Oauth的更多功能:
// REDIRECTING TO THE URL
header('Location: ' . $url);
}
请看看亚伯拉罕的推特Oauth,可以使用composer.json上传它:
{
"type": "package",
"package": {
"name": "abraham/twitteroauth",
"description": "Twitter oauth",
"version": "dev-dev",
"keywords": ["Twitter API", "Twitter oAuth"],
"license": "MIT",
"authors": [
{
"name": "Abraham Williams",
"email": "abraham@abrah.am"
}
],
"require": {
"php": ">=5.3.2"
},
"autoload": {
"files": ["twitteroauth/OAuth.php"]
},
"source": {
"type": "git",
"url": "https://github.com/abraham/twitteroauth",
"reference": "origin/dev"
}
}}]