Twitter OAuth (PHP):需要好的、基本的示例才能开始

新手上路,请多包涵

使用 Facebook 的 PHP SDK,我能够在我的网站上快速登录 Facebook。他们只是设置了一个 $user 可以很容易访问的变量。

我没有这样的运气试图让 Twitter 的 OAuth 登录正常工作……坦率地说,他们的 github 材料对于 PHP 和网页设计相对较新的人来说令人困惑和无用,更不用说我的许多非官方示例了尝试过的方法同样令人困惑或已经过时。

我真的需要一些帮助来让 Twitter 登录正常工作——我的意思只是一个基本示例,我单击登录按钮,我授权我的应用程序,然后它重定向到一个页面,在该页面上显示已登录用户的名称。

非常感谢你的帮助。

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

原文由 tnw 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

我刚刚从 github 上尝试了 abraham 的 twitteroauth,它似乎对我来说工作正常。这就是我所做的

  1. git 克隆 https://github.com/abraham/twitteroauth.git

  2. 使用域将其上传到您的虚拟主机,例如 www.example.com

  3. 转到 Twitter Apps 并注册您的应用程序。您需要的更改是(假设您将使用托管在 http://www.example.com/twitteroauth 的 abraham 的 twitteroauth 示例)

    a) 申请网站为 http://www.example.com/twitteroauth

    b) 应用程序类型将是浏览器

c) 回调 url 为 http://www.example.com/twitteroauth/callback.php (Callback.php 包含在 git 源中)

  1. 执行此操作后,您将获得 CONSUMER_KEY 和 CONSUMER_SECRET,您可以从 twitteroauth 发行版的 config.php 中更新它们。还将回调设置为与 http://www.example.com/twitteroauth/callback.php 相同

而已。如果你现在导航到 http://www.example.com/twitteroauth ,你会得到一个“Signin with Twitter”,这将带你到 Twitter ,授权请求并让你回到 index.php 页面。

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

如果您想查看工作示例,请 在此处 找到

原文由 rajasaur 发布,翻译遵循 CC BY-SA 3.0 许可协议

这是获取授权 url 然后在你从 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);
}
?>

原文由 zoomi 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题