Php 通过oAuth授权,发布到Twitter
我已经创建了一个Twitter应用程序来自动发布到我的Twitter帐户。所以,我不需要授权新用户 我已经将访问级别设置为读/写,并收到一个访问令牌。我在应用程序中使用了OAuth工具来生成cURL命令: curl--请求“POST” --数据'status=Maybe+he%27ll+finally+find+his+密钥+%23peterfalk'--header'授权:OAuth OAuth_consumer_key=“…”,OAuth_nonce=“97fad626790e8e5988d4a06cfd47fa74”,OAuth_签名=“…”, oauth_signature_method=“HMAC-SHA1”,oauth_timestamp=“1364161424”, oauth_token=“…”,oauth_version=“1.0”-详细 在命令上方,它说: 重要提示:此项仅在几分钟内有效。还记得吗 cURL命令将实际执行请求 我假设这将在linux终端中工作 我想知道如何将其转换为PHP cURL命令。这是我试过的。请注意,$DST的值是中“DST”的值;它还等于OAuth工具中cURL命令中Php 通过oAuth授权,发布到Twitter,php,curl,twitter,twitter-oauth,Php,Curl,Twitter,Twitter Oauth,我已经创建了一个Twitter应用程序来自动发布到我的Twitter帐户。所以,我不需要授权新用户 我已经将访问级别设置为读/写,并收到一个访问令牌。我在应用程序中使用了OAuth工具来生成cURL命令: curl--请求“POST” --数据'status=Maybe+he%27ll+finally+find+his+密钥+%23peterfalk'--header'授权:OAuth OAuth_consumer_key=“…”,OAuth_nonce=“97fad626790e8e5988d
--header'Authorization:
之后的字符串值
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
data => 'status='.urlencode($status),
header => 'Authorization: '.$DST
)
));
$resp = curl_exec($ch);
curl_close($ch);
但是$resp
的值是:
{
request: "/1/statuses/update.json",
error: "Could not authenticate you."
}
我做错什么了吗?注意,OAuth工具说cURL命令实际上可以工作。所以我认为这只是一个如何在PHP中安排卷曲的问题。我对它不太熟悉。还要注意,如果可以的话,我希望避免使用OAuth库。我觉得应该有一个比安装整个库轻得多的解决方案。我有三个问题:
(1) 我没有把它包括在这个问题中,但我的签名没有正确创建
我所做的是base64_编码(hash_hmac('sha1',$base,$key))
但我应该将hash_hmac第四个参数设置为true
,以返回原始二进制文件而不是十六进制(十六进制在Twitter文档中显示为示例,这让我很困惑)。因此,正确的函数是:base64_encode(hash_hmac('sha1',$base,$key,true))代码>
(2) 旋度设置不正确。我要求CURLOPT_HTTPHEADER设置授权,并将CURL_VERBOSE设置为true:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $DST));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.rawurlencode($status));
curl_setopt($ch, CURLOPT_URL, $url);
(3) 如上面代码所示,我必须将状态作为字符串而不是数组发布。我在这个问题上找到了这个问题的解决方案:
所有这些现在都运转良好。另外,确保Twitter应用程序中的访问令牌显示访问级别为“读写”。如果没有,请在“设置”中更改权限,然后返回“详细信息”并重新创建访问令牌
全文
您好,谢谢您的帮助
我也遇到了同样的问题,您可以在下面找到我的代码:
<?php
public function updateStatus($message)
{
// Encoding message for the curl data parameter
$messageData = rawurlencode($message);
// Double encoding message for the message in signature base string
$messageSignature = rawurlencode($messageData);
// URL for posting a new tweet
$statusURL = rawurlencode('https://api.twitter.com/'.$this->version.'/statuses/update.json');
// Create oauth_nonce parameter
$oauth_nonce = preg_replace('~[\W]~','',base64_encode(uniqid()));
// Create timestamp
$oauth_timestamp = time();
// Create signature base string parameter
$signature_base_string = "POST&".$statusURL."&oauth_consumer_key%3D".CB_TWITTER_API_KEY."%26oauth_nonce%3D".$oauth_nonce."%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D".$oauth_timestamp."%26oauth_token%3D".$this->sessionData['oauth_token']."%26oauth_version%3D1.0%26status%3D".$messageSignature."";
// Create signature key
$signature_key = CB_TWITTER_SECRET_KEY.'&'.$this->sessionData['oauth_token_secret'];
// Create new signature
$newSignature = rawurlencode(base64_encode(hash_hmac('sha1', $signature_base_string, $signature_key, true)));
// Create header
$header="Authorization:
OAuth
oauth_consumer_key=\"".CB_TWITTER_API_KEY."\",
oauth_nonce=\"".$oauth_nonce."\",
oauth_signature=\"".$newSignature."\",
oauth_signature_method=\"HMAC-SHA1\",
oauth_timestamp=\"".$oauth_timestamp."\",
oauth_token=\"".$this->sessionData['oauth_token']."\",
oauth_version=\"1.0\"";
// Replace line breaks and tabulations in header
$header = preg_replace( "/\r|\n|\t/", "", $header);
// Init cURL
$ch = curl_init();
// Put header
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
// Set request type to POST for POSTing a tweet
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// Enable verbose for debugging only
curl_setopt($ch, CURLOPT_VERBOSE, false);
// Return the transfer in a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Set URL of cURL request
curl_setopt($ch, CURLOPT_URL, rawurldecode($statusURL));
// Do a POST request (HTML POST)
curl_setopt($ch, CURLOPT_POST, 1);
// Set data
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.$messageData);
// Execute cURL and store result
$output = curl_exec($ch);
// Close cURL
curl_close($ch);
if ($output) {
$output = json_decode($output);
}
if ($this->hasErrors($output, false))
{
return false;
}
return true;
}
?>
希望能有所帮助。谢谢分享代码!你知道如何在Twitter上发布一张图片和状态信息吗?新年快乐你救了我一天。谢谢你分享解决方案。
<?php
public function updateStatus($message)
{
// Encoding message for the curl data parameter
$messageData = rawurlencode($message);
// Double encoding message for the message in signature base string
$messageSignature = rawurlencode($messageData);
// URL for posting a new tweet
$statusURL = rawurlencode('https://api.twitter.com/'.$this->version.'/statuses/update.json');
// Create oauth_nonce parameter
$oauth_nonce = preg_replace('~[\W]~','',base64_encode(uniqid()));
// Create timestamp
$oauth_timestamp = time();
// Create signature base string parameter
$signature_base_string = "POST&".$statusURL."&oauth_consumer_key%3D".CB_TWITTER_API_KEY."%26oauth_nonce%3D".$oauth_nonce."%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D".$oauth_timestamp."%26oauth_token%3D".$this->sessionData['oauth_token']."%26oauth_version%3D1.0%26status%3D".$messageSignature."";
// Create signature key
$signature_key = CB_TWITTER_SECRET_KEY.'&'.$this->sessionData['oauth_token_secret'];
// Create new signature
$newSignature = rawurlencode(base64_encode(hash_hmac('sha1', $signature_base_string, $signature_key, true)));
// Create header
$header="Authorization:
OAuth
oauth_consumer_key=\"".CB_TWITTER_API_KEY."\",
oauth_nonce=\"".$oauth_nonce."\",
oauth_signature=\"".$newSignature."\",
oauth_signature_method=\"HMAC-SHA1\",
oauth_timestamp=\"".$oauth_timestamp."\",
oauth_token=\"".$this->sessionData['oauth_token']."\",
oauth_version=\"1.0\"";
// Replace line breaks and tabulations in header
$header = preg_replace( "/\r|\n|\t/", "", $header);
// Init cURL
$ch = curl_init();
// Put header
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
// Set request type to POST for POSTing a tweet
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// Enable verbose for debugging only
curl_setopt($ch, CURLOPT_VERBOSE, false);
// Return the transfer in a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Set URL of cURL request
curl_setopt($ch, CURLOPT_URL, rawurldecode($statusURL));
// Do a POST request (HTML POST)
curl_setopt($ch, CURLOPT_POST, 1);
// Set data
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.$messageData);
// Execute cURL and store result
$output = curl_exec($ch);
// Close cURL
curl_close($ch);
if ($output) {
$output = json_decode($output);
}
if ($this->hasErrors($output, false))
{
return false;
}
return true;
}
?>