简体   繁体   English

为什么我的 Twitter oAuth 请求不起作用? | Curl,接头

[英]Why is my Twitter oAuth Request not Working? | Curl, Headers

    $callbackUrl = urlencode(self::CALLBACK_URL);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://api.twitter.com/oauth/request_token");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        'oauth_callback' => $callbackUrl,
        'oauth_consumer_key' => self::TWITTER_API_KEY
    ]);
    $timestamp = date("U"); // UTC UNIX time
    $oauth_nonce = preg_replace( '/[\W]/', '', base64_encode($timestamp));
    $headers = [
        "Authorization: OAuth oauth_consumer_key=\"".self::TWITTER_API_KEY."\"",
        "oauth_nonce: \"$oauth_nonce\"",
        "oauth_signature: \"oauth_signature\"",
        "oauth_signature_method: \"HMAC-SHA1\"",
        "oauth_timestamp: \"$timestamp\"",
        "oauth_version: \"1.0\"",
    ];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_STDERR, $fp);
    curl_setopt($ch, CURLINFO_HEADER_OUT , true);
    $curlResult = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Curl Request Error:' . curl_error($ch);
        throw new CHttpException(404,'ERROR');
    }
    curl_close ($ch);

The Twitter Result: {"errors":[{"code":215,"message":"Bad Authentication data."}]} Twitter 结果: {"errors":[{"code":215,"message":"Bad Authentication data."}]}

Request:要求:

    [url] => https://api.twitter.com/oauth/request_token
[content_type] => application/json; charset=utf-8
[http_code] => 400
[header_size] => 1395
[request_size] => 429
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.250341
[namelookup_time] => 0.028679
[connect_time] => 0.044366
[pretransfer_time] => 0.093077
[size_upload] => 344
[size_download] => 62
[speed_download] => 247
[speed_upload] => 1374
[download_content_length] => 62
[upload_content_length] => 344
[starttransfer_time] => 0.115261
[redirect_time] => 0
[redirect_url] => 0
[primary_ip] => 104.244.42.2
[certinfo] => Array
    (
    )

[primary_port] => 443
[local_ip] => 192.168.208.2
[local_port] => 48502
[request_header] => POST /oauth/request_token HTTP/1.1

I did everything like the Twitter Documentation but it returns 215. I think the Problem is on the headers part but i don't know what is wrong there.我做了 Twitter 文档之类的所有操作,但它返回 215。我认为问题出在标题部分,但我不知道那里有什么问题。

I generated the nonce with random base64 Stuff like Twitter said that in there Documentation.我用随机的 base64 像 Twitter 这样的东西生成了随机数,在文档中说。 The Time looks similar to the example Time from Twitter.时间看起来类似于来自 Twitter 的示例时间。 I saw when the Time is 5 minutens above twitters the Request gets blocked.我看到当时间比推特高 5 分钟时,请求被阻止。 Is that the same Error Code then?那是相同的错误代码吗?

As per documentation , Authorization header should contains all the data.根据文档,授权 header 应包含所有数据。 Your Authorization header contains only OAuth oauth_consumer_key .您的Authorization header 仅包含OAuth oauth_consumer_key

Your actual header (bad format, because, for example, 'oauth' is an entry of the headers, but should be a property of Authorization - see below).您实际的 header (格式错误,因为例如,'oauth' 是标题的条目,但应该是 Authorization 的属性 - 见下文)。

Authorization: OAuth oauth_consumer_key='xxx'
oauth_nonce: 'MTY0MzIwNjg5NQ'
oauth_signature: 'oauth_signature'
oauth_signature_method: 'HMAC-SHA1'
oauth_timestamp: '1643206895'
oauth_version: '1.0'

you should have你应该有

Authorization: OAuth oauth_consumer_key='xxx', oauth_nonce="MTY0MzIwNjg5NQ", oauth_signature="oauth_signature", ...

You need to concat all data in one string .您需要将所有数据连接到一个字符串中。 But you could create an array to help to build it.但是您可以创建一个数组来帮助构建它。 Here is an example:这是一个例子:

$authParams = implode(', ', [
    'oauth_consumer_key="' . self::TWITTER_API_KEY . '"',
    'oauth_nonce="' . $oauth_nonce . '"',
    'oauth_signature="oauth_signature"',
    'oauth_signature_method="HMAC-SHA1"',
    'oauth_timestamp="' . $timestamp . '"',
    'oauth_version="1.0"',
]);

$headers = [
    "Authorization: OAuth $authParams"
];
var_dump($headers);

Outputs something like:输出类似:

array(1) {
  [0]=>
  string(197) "Authorization: OAuth oauth_consumer_key="xxx", oauth_nonce="MTY0MzIwNjg2MA", oauth_signature="oauth_signature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1643206860", oauth_version="1.0""
}

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

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