无法使用 Abraham Twitter API SDK



我正在使用亚伯拉罕推特API SDK,在执行index.php页面时遇到一些问题。在我的配置.php页面中,我的代码是这样的

define('CONSUMER_KEY', 'xxxxxxxxxxxxxxxxxxxxxx');
define('CONSUMER_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('OAUTH_CALLBACK', 'http://example.com/callback.php');

当我在浏览器中打开索引文件时,它会重定向到连接.php在这里,如果我单击Signin with PHP按钮,页面将重定向到重定向.php在这里,我的问题来了。在重定向.php页面中,我包括twitteroauth.php文件。

我的重定向.php文件中的代码

/* Start session and load library. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
/* Save temporary credentials to session. */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
/* If last connection failed don't display authorization link. */
switch ($connection->http_code) {
  case 200:
    /* Build authorize URL and redirect user to Twitter. */
    $url = $connection->getAuthorizeURL($token);
    header('Location: ' . $url); 
    break;
  default:
    /* Show notification if something went wrong. */
    echo 'Could not connect to Twitter. Refresh the page or try again later.';
}

在我的 twitteroauth.php 文件中,构造函数看起来像这样

function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
    $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
    $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
    if (!empty($oauth_token) && !empty($oauth_token_secret)) {
      $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
    } else {
      $this->token = NULL;
    }
  }

我在浏览器中遇到的错误是

Notice: Undefined index: oauth_token in line 80
Could not connect to Twitter. Refresh the page or try again later.

我尝试了很多方法来解决这个问题。但我失败了。请帮帮我。提前谢谢。

我也有类似的错误。我通过在推特应用程序设置中设置回调 URL 解决了这个问题。http://dev.twitter.com

  • 在 https://dev.twitter.com 登录
  • 转到 https://dev.twitter.com/apps
  • 点击"设置"选项卡
  • 将"回调 URL"设置为类似于 http://example.com/twitteroauth/callback.php
  • 然后保存设置

最新更新