简体   繁体   English

Twitter 401未经PHP和Abraham Williams授权

[英]Twitter 401 not authorized PHP & Abraham Williams Twitter

I've been using Abraham Williams Twitter OAuth Library and it was a breeze setting everything up, except for one thing. 我一直在使用亚伯拉罕·威廉姆斯(Abraham Williams)的Twitter OAuth库,轻而易举地设置了所有内容,除了一件事。 I try to post a message to twitter and get a 401 not authorized. 我尝试在Twitter上发布消息,并获得未经授权的401。 I've been searching the Internet for answers, I tried getting new Consumer keys but nothing seems to work. 我一直在互联网上寻找答案,我尝试获取新的Consumer键,但似乎无济于事。

This is the code I'm using to post a test tweet to Twitter. 这是我用来将测试推文发布到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!";
}

And this is the error I get. 这就是我得到的错误。 I don't have a Callback URL. 我没有回呼网址。 Should I have one? 我应该有一个吗?

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
        )

)

I fixed it using another great library from J7mbo https://github.com/J7mbo/twitter-api-php/blob/master/README.md 我使用来自J7mbo的另一个很棒的库https://github.com/J7mbo/twitter-api-php/blob/master/README.md修复了它

Simplest PHP example for retrieving user_timeline with Twitter API version 1.1 使用Twitter API版本1.1检索user_timeline的最简单的PHP示例

I used this code to post to Twitter. 我使用此代码发布到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();

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

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