2

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

当我在本地主机上运行它时,它会愉快地返回搜索结果,但是在我的服务器上运行它会给我以下错误:

[代码] => 99 [标签] =>authentity_token_error [消息] => 无法验证您的凭据

所以它在第一次请求获取我的不记名令牌时失败了。

这对我来说似乎有点古怪,显然很难调试。我可能缺少一些非常基本的东西,所以任何帮助都会被感激地接受!

下面是我的代码:

    // 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);
4

1 回答 1

1

我实际上并没有设法解决上述问题,但是关于在发送的标头中使用字符串与数组(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>";
    }

我希望这对那里的人有用!

于 2013-08-22T14:59:29.060 回答