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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6550337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:42:16  来源:igfitidea点击:

Twitter OAuth (PHP): Need good, basic example to get started

phphtmltwittertwitter-oauth

提问by tnw

Using Facebook's PHP SDK, I was able to get Facebook login working pretty quickly on my website. They simply set a $uservariable that can be accessed very easily.

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

I've had no such luck trying to get Twitter's OAuth login working... quite frankly, their github material is confusing and useless for someone that's relatively new to PHP and web design, not to mention that many of the unofficial examples I've tried working through are just as confusing or are outdated.

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

I really need some help getting Twitter login working--I mean just a basic example where I click the login button, I authorize my app, and it redirects to a page where it displays the name of the logged in user.

我真的需要一些帮助来让 Twitter 登录工作——我的意思是一个基本的例子,我点击登录按钮,我授权我的应用程序,它重定向到一个页面,在那里它显示登录用户的姓名。

I really appreciate your help.

我真的很感谢你的帮助。

EDITI'm aware of the existence of abraham's twitter oauthbut it provides close to no instructions whatsoever to get his stuff working.

编辑我知道abraham 的 twitter oauth的存在,但它几乎没有提供任何说明来让他的东西工作。

采纳答案by rajasaur

I just tried abraham's twitteroauth from github and it seems to work fine for me. This is what I did

我刚刚从 github 尝试了 abraham 的 twitteroauth,它似​​乎对我来说很好用。这就是我所做的

  1. git clone https://github.com/abraham/twitteroauth.git
  2. Upload this into your webhost with domain, say, www.example.com
  3. Go to Twitter Appsand register your application. The changes that you need are (assuming that you will use abraham's twitteroauth example hosted at http://www.example.com/twitteroauth)
    a) Application Website will be http://www.example.com/twitteroauth
    b) Application type will be browser
    c) Callback url is http://www.example.com/twitteroauth/callback.php(Callback.php is included in the git source)
  4. Once you do this, you will get the CONSUMER_KEY and CONSUMER_SECRET which you can update in the config.php from the twitteroauth distribution. Also set the callback to be the same as http://www.example.com/twitteroauth/callback.php
  1. git 克隆https://github.com/abraham/twitteroauth.git
  2. 将其上传到您的具有域的虚拟主机中,例如 www.example.com
  3. 转到Twitter 应用程序并注册您的应用程序。您需要的更改是(假设您将使用托管在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 源中)
  4. 完成此操作后,您将获得 CONSUMER_KEY 和 CONSUMER_SECRET,您可以在 twitteroauth 发行版的 config.php 中更新它们。还将回调设置为与http://www.example.com/twitteroauth/callback.php相同

Thats it. If you now navigate to http://www.example.com/twitteroauth, you will get a "Signin with Twitter", that will take you to Twitter , authorize the request and get you back to the index.php page.

就是这样。如果您现在导航到http://www.example.com/twitteroauth,您将获得“使用 Twitter 登录”,它将带您到 Twitter ,授权请求并让您返回 index.php 页面。

EDIT: Example will not work but do not worry. Follow the above steps and upload to server. Make sure you rename the file from github repository i.e. config-sample.php->config.php

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

if you want to see a working sample, find it here

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

回答by zoomi

this one is the basic example of getting the url for authorization and then fetching the user basic info when once u get back from twitter

这是获取 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); 
}
?>

回答by muni

Here is the step by step guide to integrate Twitter OAuth API to Web-application using PHP. Please following tutorial.

这是使用 PHP 将 Twitter OAuth API 集成到 Web 应用程序的分步指南。请按照教程。

http://www.smarttutorials.net/sign-in-with-twitter-oauth-api-using-php/

http://www.smarttutorials.net/sign-in-with-twitter-oauth-api-using-php/

You need to create Twitter App First By going thorugh following URL

您需要首先通过以下 URL 创建 Twitter 应用程序

https://apps.twitter.com/

https://apps.twitter.com/

Then you need to provide necessary information for the twitter app. Once your provided all the information and then save it. You will get Twitter application Consumer Key and Consumer secret.

然后您需要为 twitter 应用程序提供必要的信息。一旦您提供了所有信息,然后保存。您将获得 Twitter 应用程序 Consumer Key 和 Consumer secret。

Please download the source file from above link, and just replace TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET and TWITTER_OAUTH_CALLBACK with your Consumer Key (API Key), Consumer Secret (API Secret) and callback URL. Then upload this to your server. Now it will work successfully.

请从上面的链接下载源文件,只需将 TWITTER_CONSUMER_KEY、TWITTER_CONSUMER_SECRET 和 TWITTER_OAUTH_CALLBACK 替换为您的消费者密钥(API 密钥)、消费者秘密(API 秘密)和回调 URL。然后将其上传到您的服务器。现在它将成功运行。

回答by Jon

Here are some OAuth 1.0A PHP libraries with examples:

下面是一些带有示例的 OAuth 1.0A PHP 库:

Twitter async provides documentationon how to simply sign in a user as you asked for.

Twitter async 提供了有关如何按照您的要求简单地登录用户的文档

回答by Felix Turner

Abraham's Twitteroauthhas a working demo here: https://github.com/abraham/twitteroauth-demo

亚伯拉罕的Twitteroauth在这里有一个工作演示:https: //github.com/abraham/twitteroauth-demo

Following the steps in the demo readme worked for me. In order to run composer on macOS I had to do this after installingit: mv composer.phar /usr/local/bin/composer

按照演示自述文件中的步骤对我有用。为了在 macOS 上运行 composer,我必须在安装后执行以下操作:mv composer.phar /usr/local/bin/composer

IMO the demo could be a lot simpler and should be included in the main twitteroauth repo.

IMO 演示可能要简单得多,应该包含在主要的 twitteroauth 存储库中。