Twitter应用程序只有auth可以在本地主机上运行,但不能在服务器上运行


Twitter app-only auth works on localhost, but not on server

我正在为Twitter使用仅限应用程序的身份验证,并试图获得搜索结果。我大部分时间都在使用Codeigniter,但有问题的代码只是普通的PHP。

当我在本地主机上运行它时,它很高兴地返回搜索结果,但在服务器上运行它会出现以下错误:

[代码]=>99[label]=>authenticity_token_error[message]=>无法验证您的凭据

所以它在第一次请求获得我的持有者代币时失败了。

这在我看来有点古怪,而且很难调试。我可能错过了一些非常基本的东西,所以任何帮助都会被感激地接受!

以下是我的代码:

    // get consumer key and consumer secret from my config files
    $consumer = array(
        'key' => $this->config->item($provider.'_key'),
        'secret' => $this->config->item($provider.'_secret'),
    );
    // encode it into the format Twitter expects
    $bearerTokenCred = base64_encode($consumer['key'].":".$consumer['secret']);
    // set up request
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  =>    'Authorization: Basic '.$bearerTokenCred."'r'n".
                            "Content-type: application/x-www-form-urlencoded;charset=UTF-8'r'n",
            'content' => 'grant_type=client_credentials'
        )
    );
    $context  = stream_context_create($opts);
            // send request
    $result = file_get_contents('https://api.twitter.com/oauth2/token', false, $context);
    $result2 = json_decode($result, true);
            // this is where it prints my error - prior to doing anything else
    print_r($result2);
    if ($result2["token_type"] != "bearer"){
        echo $result2["token_type"];
        return;
    }
    $opts = array('http' =>
        array(
            'method'  => 'GET',
            'header'  => 'Authorization: Bearer '.$result2["access_token"]       
        )
    );
    $context  = stream_context_create($opts);
    $result3 = file_get_contents('https://api.twitter.com/1.1/search/tweets.json?q='.urlencode($search), false, $context);

我实际上并没有像前面所说的那样解决这个问题,但在发送的标头中使用字符串与数组的问题上进行了一些研究(http://www.php.net/manual/en/function.stream-context-create.php#99353)-这对我仍然没有任何帮助。

最后,我求助于使用CURL来完成这项工作。

我的假设是这与我的服务器配置有关,但我不能告诉你什么!

以下的最终工作代码

    $provider = 'twitter';
    // Get keys from the config
    $consumer = array(
        'key' => $this->config->item($provider.'_key'),
        'secret' => $this->config->item($provider.'_secret'),
    );
    $bearerTokenCred = base64_encode($consumer['key'].":".$consumer['secret']);
    $data = array ('grant_type' => 'client_credentials');
    $data = http_build_query($data);
    $header = array(
        "Authorization: Basic $bearerTokenCred",
        "Content-type: application/x-www-form-urlencoded;charset=UTF-8",
        "Content-Length: " . strlen($data) 
    );
    $options = array( 
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_HEADER => false,
        CURLOPT_URL => "https://api.twitter.com/oauth2/token",
        CURLOPT_RETURNTRANSFER => true
    );
    $options[CURLOPT_POSTFIELDS] = $data;
    $feed = curl_init();
    curl_setopt_array($feed, $options);
    $result = curl_exec($feed);
    curl_close($feed);
    $result2 = json_decode($result, true);
    if ($result2["token_type"] != "bearer"){
        echo "error - invalid token type";
        return;
    }
    $header = array(
        'Authorization: Bearer '.$result2["access_token"]
    );
    $options = array( 
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_HEADER => false,
        CURLOPT_URL => 'https://api.twitter.com/1.1/search/tweets.json?q='.urlencode('"#'.$search.'"'),
        CURLOPT_RETURNTRANSFER => true
    );
    $feed = curl_init();
    curl_setopt_array($feed, $options);
    $result = curl_exec($feed);
    curl_close($feed);
    $result3 = json_decode($result, true);
    foreach($result3['statuses'] as $status){
        echo "<p>".$status['text']."</p>";
    }

我希望这对外面的人有用!