我试图为Twitter rest api做application only authentication flow。我从命令行触发这个curl来获取我的twitter api bearer token:
curl --request 'POST' 'https://api.twitter.com/oauth2/token' --header 'Authorization: Basic b... ... ...==' --header 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' --data "grant_type=client_credentials" --verbose
我从Twitter收到了持票人令牌,但当我尝试通过php发出相同的请求时,我收到一个错误:
{"errors":[{"code":99,"message":"Unable to verify your credentials","label":"authenticity_token_error"}]}
我写的命令行curl与我写的php有什么区别?
//cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/oauth2/token');
//curl_setopt($ch, CURLOPT_USERAGENT, "TwitterAUth/1.0");
// Include header in result? (0 = yes, 1 = no)
//curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
//curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization" => sprintf("Basic %s", $encoded_bearer_cred),
"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8",
));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$output = curl_exec($ch);
curl_close($ch);
echo $output;