简体   繁体   English

使用Twitter OAuth API进行身份验证

[英]Authenticate with Twitter OAuth API

I am currently trying to integreate twitter into a php web app that I am working on with OAuth. 我目前正在尝试将twitter集成到我正在使用OAuth的php web应用程序中。

I have an HTML page which provides a link to the twitter app authentication url which appears to be working fine and is showing the authentication screen. 我有一个HTML页面,提供了一个链接到twitter应用程序验证URL,它似乎工作正常,并显示验证屏幕。

Below is the code that calls the function. 下面是调用该函数的代码。

if (!isset($_GET['oauth_token']))
{
    //include("phpHandler/twitterLib/secret.php");
    getTwitterURL($consumer_key, $consumer_secret);
}

The consumer_key and consumer_secret are included within a php file. consumer_key和consumer_secret包含在php文件中。

Below is the code that gets the twitter authorisation url. 以下是获取twitter授权网址的代码。

function getTwitterUrl($consumer_key, $consumer_secret)
{
    $twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
    $url = $twitterObj->getAuthorizationUrl();
    echo '<a class="linkButtons" href="'.$url.'">Add Twitter</a>';
}

This redirect back to the page fine and then I call the authentication method to retrieve info like twitter username. 这个重定向回页面很好,然后我调用身份验证方法来检索像twitter用户名的信息。 Below is the function that does the authentication 以下是执行身份验证的功能

function authenticate($consumer_key, $consumer_secret)
{
    require ("twitterLib/EpiCurl.php");
    require ("twitterLib/EpiOAuth.php");
    require ("twitterLib/EpiTwitter.php");
    require ("twitterLib/secret.php");*/
    $twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
    $twitterObj->setToken($_GET['oauth_token']);
    $token = $twitterObj->getAccessToken();
    $twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
    $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
    $token = $twitterObj->getAccessToken();
    $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
    $_SESSION['ot'] = $token->oauth_token;
    $_SESSION['ots'] = $token->oauth_token_secret;
    $twitterInfo= $twitterObj->get_accountVerify_credentials();
    echo '<pre>';
    print_r($twitterInfo->response);
  }

The echo and print_r is to show the response return from twitter. echo和print_r用于显示来自twitter的响应返回。

I am getting the following error printed out in the array 我在阵列中打印出以下错误

Array ( [error] => Invalid / expired Token [request] => /account/verify_credentials.json ) 数组([错误] =>无效/过期令牌[请求] => /account/verify_credentials.json)

How can I fix this error. 我该如何解决这个错误。 I don't know why its invalid or expired, I have closed the browser and started again but get the same error appear. 我不知道为什么它无效或过期,我已关闭浏览器并重新启动但出现相同的错误。

Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

Your access token will be invalid if a user explicitly rejects your application from their settings or if a Twitter admin suspends your application. 如果用户明确拒绝其设置中的应用程序或Twitter管理员暂停您的应用程序,则您的访问令牌将无效。 If your application is suspended there will be a note on your application page saying that it has been suspended. 如果您的申请被暂停,您的申请页面上会有一条说明已被暂停的说明。

Many users trust an application to read their information but not necessarily change their name or post new statuses. 许多用户信任应用程序以读取其信息,但不一定更改其名称或发布新状态。 Updating information via the Twitter API - be it name, location or adding a new status - requires and HTTP POST. 通过Twitter API更新信息 - 无论是名称,位置还是添加新状态 - 都需要和HTTP POST。 We stuck with the same restriction when implementing this. 在实施时我们坚持使用相同的限制。 Any API method that requires an HTTP POST is considered a write method and requires read & write access. 任何需要HTTP POST的API方法都被视为写入方法,需要读写访问权限。

Whatever your storage system may be, you'll need to begin storing an oauth_token and oauth_token_secret (collectively, an "access token") for each user of your application. 无论您的存储系统是什么,您都需要为应用程序的每个用户开始存储oauth_token和oauth_token_secret(统称为“访问令牌”)。 The oauth_token_secret should be stored securely. 应该安全地存储oauth_token_secret。 Remember, you'll be accessing these values for every authenticated request your application makes to the Twitter API, so store them in a way that will scale to your user base. 请记住,您将为应用程序对Twitter API发出的每个经过身份验证的请求访问这些值,因此请以适合您的用户群的方式存储它们。 When you're using OAuth, you should no longer be storing passwords for any of your users. 当您使用OAuth时,您不应再为任何用户存储密码。

require '../tmhOAuth.php';

require '../tmhUtilities.php';

$tmhOAuth = new tmhOAuth(array(

  'consumer_key'    => 'YOUR_CONSUMER_KEY',

  'consumer_secret' => 'YOUR_CONSUMER_SECRET',

  'user_token'      => 'AN_ACCESS_TOKEN',

  'user_secret'     => 'AN_ACCESS_TOKEN_SECRET',

));



// we're using a hardcoded image path here. You can easily replace this with an uploaded image-see images.php example)

// 'image = "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",



$image = "./dickvandyke.jpg';



$code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json',

  array(

    'media[]'  => "@{$image}",

    'status'   => "Don't slip up" // Don't give up..

  ),

  true, // use auth

  true  // multipart

);



if ($code == 200) {

  tmhUtilities::pr(json_decode($tmhOAuth->response['response']));

} else {

  tmhUtilities::pr($tmhOAuth->response['response']);

}

I worked on new Twitter API. 我参与了新的Twitter API。 It is working fine for me with following code I did. 我用以下代码对我工作正常。

<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$consumer_key = "XXXXXXX";
$consumer_secret = "XXXXXXX";

$connection = new TwitterOAuth($consumer_key, $consumer_secret);

 $request_token= $connection->oauth('oauth/request_token', array('oauth_callback' => "http://callbackurlhere.com/callback.php"));
$url = $connection->url("oauth/authorize", array("oauth_token" => $request_token['oauth_token']));

header('Location: '. $url);
?>

callback.php code below to obtain the permanent oauthToken and save it in database for further use: 下面的callback.php代码获取永久oauthToken并将其保存在数据库中以供进一步使用:

<?php
require "vendor/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;

    // session_start();
    if(isset($_REQUEST['oauth_verifier'])){

    $oauth_access_token = $_REQUEST['oauth_token'];
    $oauth_access_token_secret = $_REQUEST['oauth_verifier'];
    $consumer_key = "XXXXXXXXXXXXXXXX";
    $consumer_secret = "XXXXXXXXXXXXXXX";
    $connection = new TwitterOAuth($consumer_key, $consumer_secret,$oauth_access_token , $oauth_access_token_secret );

    $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_access_token_secret));
    var_dump($access_token); die("--success here--");// Obtain tokens and save it in database for further use.
    }

    ?>

I've managed to find the problem. 我设法找到了问题。 I always creating two new EpiTwitter objects in the authenticate function. 我总是在authenticate函数中创建两个新的EpiTwitter对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM