Php Twitter API错误“;缺少必需参数:grant“U type”;使用Unirest.io

Php Twitter API错误“;缺少必需参数:grant“U type”;使用Unirest.io,php,twitter-oauth,unirest,Php,Twitter Oauth,Unirest,我正在尝试使用Unirest.io PHP从Twitter获取API令牌。我的代码如下: [in PHP] $response = Unirest::post("https://api.twitter.com//oauth2/token", array( "Authorization" => "Basic [AUTH_KEY]", "Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8" ), arra

我正在尝试使用Unirest.io PHP从Twitter获取API令牌。我的代码如下:

[in PHP]
$response = Unirest::post("https://api.twitter.com//oauth2/token",
  array(
"Authorization" => "Basic [AUTH_KEY]",
"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8"
),
array("grant_type" => "client_credentials")

);
我从Twitter上得到的是:

{
 errors: [
 {
 code: 170,
 label: "forbidden_missing_parameter",
 message: "Missing required parameter: grant_type"
  }
 ]
 }

据我所知,它要求请求的“主体”包含“grant_type”:“client_credentials”,我认为它包含在上面的unirest请求中,但显然不是这样。有什么帮助或意见吗?

这来得很晚,但将来可能会帮助其他人。前一段时间有过这个问题,但下面是我如何解决它的。基本上,我将“grant_type”作为字符串传递,而不是像这样的数组

$uri = "https://api.twitter.com/oauth2/token";

$headers = [
    "Authorization: Basic ".XXXXXXX, 
    "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
];

$verb = "POST";

//here is where i replaced the value of body with a string instead of an array
$body = 'grant_type=client_credentials';

$ch = curl_init($uri);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $verb);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HTTP_CODE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

$result = curl_exec($ch);
$response = json_decode($result);

它返回了twitter记录的预期结果。

为什么要将其放在单独的数组中?POST请求的Unirest语法是:Unirest::POST($url,$headers=array(),$body=NULL,$username=NULL,$password=NULL),而twitter API要求grant_类型必须位于请求的正文中。我也尝试在headers数组中使用它,错误消息是相同的。