Twitter API返回错误代码32

时间:2018-07-09 13:55:30

标签: php twitter oauth

我们当前正在使用Twitter API,但未能成功向其API发送推文。它总是在Java和PHP中都以代码32退出(将PHP原型制成只是用于发送请求)。我想我在这里缺少有关OAuth1规范的信息。

我们正在使用的PHP原型:

<?PHP
require_once("cURL.php");
$headers = [];
$postParams = [];
$signatureParams = [];
$authString = "";
$URL = "https://api.twitter.com/1.1/statuses/update.json";
$method = "POST";
$token = "someValidToken";
$tokenSecret = "someValidTokenSecret";
$timestamp = time();

$status = "Hi, Twitter!";
$signatureParams['status'] = $status;

$clientID = "someClientID";
$clientSecret = "someClientSecret";

$signatureParams['oauth_consumer_key'] = urlencode($clientID);
$signatureParams['oauth_nonce'] = urlencode(base64_encode($timestamp));
$signatureParams['oauth_signature_method'] = urlencode("HMAC-SHA1");
$signatureParams['oauth_timestamp'] = $timestamp;
$signatureParams['oauth_token'] = urlencode($token);    
$signatureParams['oauth_version'] = urlencode("1.0");   

$oauthSignature = generateSignature();
$signatureParams["oauth_signature"] = $oauthSignature;  
unset($signatureParams['status']);
ksort($signatureParams);

$count = 0;
$authStringLoop = "";
foreach($signatureParams as $key => $signatureParam)
{
    if($count++ == 0)
    {
        $authStringLoop .= "OAuth ";
    }

$authStringLoop .= urlencode($key);
$authStringLoop .= "=\"";   
$authStringLoop .= urlencode($signatureParam)."\",";    

}

$authStringLoop = substr($authStringLoop,0,strlen($authStringLoop)-1);

echo "</br></br>";
echo  "AuthString loop: $authStringLoop</br></br>";


$headers = array(
    "Authorization: $authStringLoop"
);

var_dump($headers).'</br>';

var_dump(POST($URL,json_encode($status),$headers,0));



function generateSignature()
{

    global $clientSecret;
    global $signatureParams;
    global $URL;
    global $method;
    global $clientSecret;
    global $clientID;
    global $tokenSecret;

    ksort($signatureParams);

    $parameterString = "";

    foreach($signatureParams as $key => $signatureParam)
    {
        $parameterString .= urlencode($key."=".$signatureParam."&");
    }

    $signingBase = strtoupper($method)."&".urlencode($URL)."&".$parameterString;
    $signingKey = urlencode($clientSecret)."&".urlencode($tokenSecret);

    var_dump($signingBase);
    echo '</br>';

    echo "Base64 encoded: ".base64_encode(hash_hmac("SHA1",$signingBase,$signingKey,TRUE));
    echo '</br>';

    echo "URL encoded: ".urlencode(base64_encode(hash_hmac("SHA1",$signingBase,$signingKey,TRUE)))."</br>";

    return base64_encode(hash_hmac("SHA1",$signingBase,$signingKey,TRUE));

}   
?>

我猜想签名生成方式有问题。但是,我发现的只是按字母顺序对它进行排序,应该使用kso​​rt()调用来完成。我们在这里想念什么?

0 个答案:

没有答案