Twitter OAuth(PHP):需要良好的基本示例才能入门

tnw*_*tnw 34 html php twitter twitter-oauth

使用Facebook的PHP SDK,我能够在我的网站上快速登录Facebook登录.他们只需设置一个$user可以轻松访问的变量.

我试图让Twitter的OAuth登录工作没有运气......坦率地说,他们的github材料对于那些对PHP和网页设计来说相对较新的人来说是混乱和无用的,更不用说许多非官方的例子了.尝试工作同样令人困惑或过时.

我真的需要一些帮助让Twitter登录工作 - 我的意思只是一个基本的例子,我点击登录按钮,我授权我的应用程序,它重定向到一个页面,其中显示登录用户的名称.

我非常感谢你的帮助.

编辑我知道亚伯拉罕的推特oauth的存在,但它提供了几乎没有任何指示让他的东西工作.

raj*_*aur 28

我刚从github尝试了亚伯拉罕的twitteroauth,它似​​乎对我来说很好.这就是我做的

  1. git clone https://github.com/abraham/twitteroauth.git
  2. 将其上传到您的域名网站,例如www.example.com
  3. 转到Twitter应用程序并注册您的应用程序.你所需要的变化(假设你会使用亚伯拉罕在托管twitteroauth的例子http://www.example.com/twitteroauth)
    一)报名网站将http://www.example.com/twitteroauth
    二)适用类型将是浏览器
    c)回调网址是http://www.example.com/twitteroauth/callback.php(Callback.php包含在git源代码中)
  4. 执行此操作后,您将获得CONSUMER_KEY和CONSUMER_SECRET,您可以通过twitteroauth发行版在config.php中更新.同时将回调设置为与http://www.example.com/twitteroauth/callback.php相同

而已.如果你现在浏览到http://www.example.com/twitteroauth,你会得到一个"签到随着Twitter的",将带你到Twitter,授权请求,并让你回到index.php页面.

编辑:示例将无法工作,但不要担心.按照上述步骤上传到服务器.确保从github存储库重命名该文件,即config-sample.php-> config.php

如果你想看一个工作样本,请在这里找到它

  • 它存储为StdClass(PHP中使用的Generic Empty类).您可以将屏幕名称作为$ content - > {screen_name}访问 (3认同)
  • 两个例子都失败了. (3认同)
  • 此答案不适用于截至 2020 年 1 月最新的 twitteroauth 存储库。 (2认同)

小智 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)

  • 这很好用.字面上只是复制和粘贴更新了消费者密钥并返回了网址,并按预期运行. (4认同)