tnw*_*tnw 34 html php twitter twitter-oauth
使用Facebook的PHP SDK,我能够在我的网站上快速登录Facebook登录.他们只需设置一个$user可以轻松访问的变量.
我试图让Twitter的OAuth登录工作没有运气......坦率地说,他们的github材料对于那些对PHP和网页设计来说相对较新的人来说是混乱和无用的,更不用说许多非官方的例子了.尝试工作同样令人困惑或过时.
我真的需要一些帮助让Twitter登录工作 - 我的意思只是一个基本的例子,我点击登录按钮,我授权我的应用程序,它重定向到一个页面,其中显示登录用户的名称.
我非常感谢你的帮助.
编辑我知道亚伯拉罕的推特oauth的存在,但它提供了几乎没有任何指示让他的东西工作.
raj*_*aur 28
我刚从github尝试了亚伯拉罕的twitteroauth,它似乎对我来说很好.这就是我做的
而已.如果你现在浏览到http://www.example.com/twitteroauth,你会得到一个"签到随着Twitter的",将带你到Twitter,授权请求,并让你回到index.php页面.
编辑:示例将无法工作,但不要担心.按照上述步骤上传到服务器.确保从github存储库重命名该文件,即config-sample.php-> config.php
如果你想看一个工作样本,请在这里找到它
小智 28
这个是获取授权网址的基本示例,然后在您从twitter返回时获取用户基本信息
<?php
session_start();
//add autoload note:do check your file paths in autoload.php
require "ret/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//this code will run when returned from twiter after authentication
if(isset($_SESSION['oauth_token'])){
$oauth_token=$_SESSION['oauth_token'];unset($_SESSION['oauth_token']);
$consumer_key = 'your consumer key';
$consumer_secret = 'your secret key';
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
//necessary to get access token other wise u will not have permision to get user info
$params=array("oauth_verifier" => $_GET['oauth_verifier'],"oauth_token"=>$_GET['oauth_token']);
$access_token = $connection->oauth("oauth/access_token", $params);
//now again create new instance using updated return oauth_token and oauth_token_secret because old one expired if u dont u this u will also get token expired error
$connection = new TwitterOAuth($consumer_key, $consumer_secret,
$access_token['oauth_token'],$access_token['oauth_token_secret']);
$content = $connection->get("account/verify_credentials");
print_r($content);
}
else{
// main startup code
$consumer_key = 'your consumer key';
$consumer_secret = 'your secret key';
//this code will return your valid url which u can use in iframe src to popup or can directly view the page as its happening in this example
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
$temporary_credentials = $connection->oauth('oauth/request_token', array("oauth_callback" =>'http://dev.crm.alifca.com/twitter/index.php'));
$_SESSION['oauth_token']=$temporary_credentials['oauth_token']; $_SESSION['oauth_token_secret']=$temporary_credentials['oauth_token_secret'];$url = $connection->url("oauth/authorize", array("oauth_token" => $temporary_credentials['oauth_token']));
// REDIRECTING TO THE URL
header('Location: ' . $url);
}
?>
Run Code Online (Sandbox Code Playgroud)