Answer the question
In order to leave comments, you need to log in
How is oauth_signature twitter api v2 formed?
I want to publish a post through the method /2/tweets
, I did everything fine, but I don’t understand how to generate oauth_signature
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
$url = "https://api.twitter.com/2/tweets";
$oauth_access_token = "токен";
$oauth_access_token_secret = "секрет токен";
$consumer_key = "consumer_key токен";
$consumer_secret = "consumer секрет токен";
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_token' => $oauth_access_token,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_nonce' => time(),
'oauth_version' => '1.0');
// Тут что-то не правильно, не правильный oauth_signature формирую
$base_info = buildBaseString($url, 'POST', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
// Публикация поста
$array = array(
'text' => 'test new post'
);
$request = tweets('https://api.twitter.com/2/tweets', $array);
print_r($request);
$header = array(buildAuthorizationHeader($oauth), 'Content-type: application/json');
// Функция постинга твита
function tweets($url, $post) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Answer the question
In order to leave comments, you need to log in
Take it and use it to your health, and do not waste time writing what has already been written 100 times before you
https://github.com/abraham/twitteroauth
But if you really want an answer to a question, then it is here
https://github.com/abraham /twitteroauth/blob/main/...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question