簡體   English   中英

如何在 PHP CURL 中重現 OAuth1.0 Request Headers

[英]How to reproduce OAuth1.0 Request Headers in PHP CURL

我正在嘗試使用 PHP 7.4 在網絡服務器上發出 Twitter verify_credentials 請求。

只有當我在 Postman 中設置 OAuth1.0 請求標頭設置時,我才會得到 http 200 代碼和正確的響應:

我如何在郵遞員中提出請求並且它有效

使用相同數據發出請求的任何其他方式都會返回錯誤 401 http 狀態碼

{"errors":[{"code":32,"message":"Could not authenticate you."}

我需要在 GUZZLE 的 PHP CURL 或另一個 http 請求客戶端代碼中轉換此 Postman 設置。 但是當我從 Postman 導入 CURL 個示例時,它總是拋出相同的 401 異常。 所以我嘗試了不同的方法:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.twitter.com/1.1/account/verify_credentials.json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Authorization: OAuth oauth_consumer_key=\"oauth_consumer_key\",oauth_token=\"oauth_token\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1605187800\",oauth_nonce=\"hmkiezWh6xqlfJYpK55rDVgcGydQkuBH\",oauth_version=\"1.0\",oauth_callback=\"http%3A%2F%2Fmyurl.com\",oauth_signature=\"signature\""
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

或者另一個:

        use GuzzleHttp\Client;
        use GuzzleHttp\Psr7\Request;

        $client = new Client;
        $headers =[
            'Authorization' => 'OAuth oauth_consumer_key="oauth_consumer_key",oauth_token="oauth_token",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1605187800",oauth_nonce="hmkiezWh6xqlfJYpK55rDVgcGydQkuBH",oauth_version="1.0",oauth_callback="http%3A%2F%2Furl.com",oauth_signature="SdB60Nr6AhJzOdAIWlW%2FwdmeJM4%3D"',
        ];
        $request = new Request('GET', 'https://api.twitter.com/1.1/account/verify_credentials.json', $headers);
        $client->send($request);
        $response = $client->getResponse();
        echo $response->getBody();

或者那樣:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/1.1/account/verify_credentials.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');


$headers = array();
$headers[] = 'Authorization: OAuth oauth_consumer_key=\"oauth_consumer_key\",oauth_token=\"oauth_token\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1605187800\",oauth_nonce=\"hmkiezWh6xqlfJYpK55rDVgcGydQkuBH\",oauth_version=\"1.0\",oauth_callback=\"http%3A%2F%2Furl.com\",oauth_signature=\"H%2FpmcdPUnlMD8RN42RpfBs%2Fs7Cc%3D\"';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

每次都會收到 401 錯誤。 那么,如何在 PHP CURL 中設置所有 OAuth1.0 屬性,以使用在 Postman 中工作的相同標頭重現相同的請求?

PS 我已經嘗試過 abraham/twitteroauth、laravel/socialite 和其他解決方案,結果相同

如果你使用的是guzzle 6以上版本,那么可以直接使用guzzle自己創建的guzzlehttp/oauth-subscriber package來處理(否則是一個漫長的過程),

將以下內容添加到您的 composer.json:

{
    "require": {
        "guzzlehttp/oauth-subscriber": "0.4.*"
    }
}

我從他們的文檔中獲取了示例,

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;

$stack = HandlerStack::create();

$middleware = new Oauth1([
    'consumer_key'    => 'my_key',
    'consumer_secret' => 'my_secret',
    'token'           => 'my_token',
    'token_secret'    => 'my_token_secret'
]);
$stack->push($middleware);

$client = new Client([
    'base_uri' => 'https://api.twitter.com/1.1/',
    'handler' => $stack
]);

// Set the "auth" request option to "oauth" to sign using oauth
$res = $client->get('account/verify_credentials.json', ['auth' => 'oauth']);

您可以關注 guzzle guzzle/oauth-subscriber文檔( https://github.com/guzzle/oauth-subscriber ) 以獲取更多信息。

原因不在 CURL 選項中,而是在無效簽名中,因為據我所知,它取決於時間戳。 所以我不能使用一個帶有不同時間戳的簽名

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM