1

我一直在使用 Abraham Williams Twitter OAuth Library,除了一件事之外,一切都变得轻而易举。我尝试在 Twitter 上发布消息并获得 401 未授权。我一直在互联网上寻找答案,我尝试获取新的消费者密钥,但似乎没有任何效果。

这是我用来向 Twitter 发布测试推文的代码。

// Connection
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);

// Posten
$connection->post('statuses/update', array('status' => "Dit is een tweet via de Twitter API"));
// Error afhandeling
$httpc = $connection->http_code;
if($httpc == 200) {
    echo 'Tweet posted!';
} else {
    echo "Fail!";
}

这是我得到的错误。我没有回调 URL。我应该有一个吗?

TwitterOAuth Object
(
    [http_code] => 401
    [url] => https://api.twitter.com/1.1/statuses/update.json
    [host] => https://api.twitter.com/1.1/
    [timeout] => 30
    [connecttimeout] => 30
    [ssl_verifypeer] => 
    [format] => json
    [decode_json] => 1
    [http_info] => Array
        (
            [url] => https://api.twitter.com/1.1/statuses/update.json
            [content_type] => application/json; charset=utf-8
            [http_code] => 401
            [header_size] => 918
            [request_size] => 519
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 0.652405
            [namelookup_time] => 0.006853
            [connect_time] => 0.108722
            [pretransfer_time] => 0.330628
            [size_upload] => 331
            [size_download] => 107
            [speed_download] => 164
            [speed_upload] => 507
            [download_content_length] => 107
            [upload_content_length] => 331
            [starttransfer_time] => 0.652388
            [redirect_time] => 0
            [certinfo] => Array
                (
                )

            [redirect_url] => 
        )

    [useragent] => TwitterOAuth v0.2.0-beta2
    [sha1_method] => OAuthSignatureMethod_HMAC_SHA1 Object
        (
        )

    [consumer] => OAuthConsumer Object
        (
            [key] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            [secret] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            [callback_url] => 
        )

    [token] => OAuthConsumer Object
        (
            [key] => xxxxxxxxxxxxxxxxxxxxxxxxx
            [secret] => xxxxxxxxxxxxxxxxxxxxxxxx
            [callback_url] => 
        )

    [http_header] => Array
        (
            [cache_control] => no-cache, max-age=300
            [content_length] => 107
            [content_type] => application/json; charset=utf-8
            [date] => Wed, 31 Jul 2013 07:03:19 GMT
            [expires] => Wed, 31 Jul 2013 07:08:19 GMT
            [server] => tfe
            [set_cookie] => guest_id=v1%3A137525419888158376; Domain=.twitter.com; Path=/; Expires=Fri, 31-Jul-2015 07:03:19 UTC
            [status] => 401 Unauthorized
            [strict_transport_security] => max-age=631138519
            [vary] => Accept-Encoding
            [www_authenticate] => OAuth realm="https://api.twitter.com"
            [x_frame_options] => SAMEORIGIN
            [x_transaction] => 8d1700c605af39d5
        )

)
4

1 回答 1

2

我使用来自 J7mbo https://github.com/J7mbo/twitter-api-php/blob/master/README.md的另一个很棒的库修复了它

使用 Twitter API 1.1 版检索 user_timeline 的最简单 PHP 示例

我使用此代码发布到 Twitter。

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';

$postfields = array(
    'status' => 'Test'
);


/** Perform the request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
             ->setPostfields($postfields)
             ->performRequest();
于 2013-08-01T08:41:42.363 回答