问题描述
我尝试使用Twitter API大约2天来发布推文,但仍然无法连接到它。有人可以在以下脚本中找到我所犯的错误吗?所有凭据都是正确的,并且可以在POSTMAN上正常工作。但是我自己的脚本不起作用!
错误
它返回{“错误”:[{“代码”:215,“消息”:“错误的身份验证数据。”}]}
我的代码
<?php
$token = 'xxxxxx';
$token_secret = 'xxxxxx';
$consumer_key = 'xxxxx';
$consumer_secret = 'xxxxxxx';
$randomstr = (string) time();
$str = "POST&" . rawurlencode("https://api.twitter.com/1.1/statuses/update.json") . "&" . rawurlencode("include_entities=true&oauth_consumer_key=$consumer_key&oauth_nonce=$randomstr&oauth_signature_method=HMAC-SHA1&oauth_timestamp=$randomstr&oauth_token=$token&oauth_version=1.0&status=TwitterAPI");
$signing_key = $consumer_secret."&".$token_secret;
$signature = base64_encode(hash_hmac('sha1',$str,$signing_key,true));
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_URL => "https://api.twitter.com/1.1/statuses/update.json?status=TwitterAPI",CURLOPT_RETURNTRANSFER => true,CURLOPT_ENCODING => "",CURLOPT_MAXREDIRS => 10,CURLOPT_TIMEOUT => 0,CURLOPT_FOLLOWLOCATION => true,CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,CURLOPT_CUSTOMREQUEST => "POST",CURLOPT_HTTPHEADER => array(
"Authorization: OAuth oauth_consumer_key=\"$consumer_key\",oauth_token=\"$token\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"$randomstr\",oauth_nonce=\"$randomstr\",oauth_version=\"1.0\",oauth_signature=\"$signature\""
),CURLOPT_POSTFIELDS => 'status=' . rawurlencode("TwitterAPI")
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)