为什么我的 Twitter oAuth 请求不起作用? |卷曲,Headers

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

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

要求:

    [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

我做了所有类似 Twitter 文档的事情,但它 returns 215。 我认为问题出在 headers 部分,但我不知道那里出了什么问题。

我用随机的 base64 生成了随机数,就像 Twitter 在文档中说的那样。 The Time 看起来类似于来自 Twitter 的示例 Time。 我看到当时间比推特高出 5 分钟时,请求被阻止。那是相同的错误代码吗?

根据 documentation,授权 header 应包含所有数据。您的 Authorization header 仅包含 OAuth oauth_consumer_key.

您实际的 header(格式错误,因为,例如,'oauth' 是 header 的条目,但应该是 属性 的授权 -见下文)。

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'

你应该

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

您需要将所有数据连接到一个字符串中。但是您可以创建一个数组来帮助构建它。这是一个例子:

$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);

输出类似:

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""
}