![](/img/trans.png)
[英]Google API OAuth 2.0 CURL returning “Required parameter is missing: grant_type”
[英]Twitter API error “Missing required parameter: grant_type” using Unirest.io
我試圖使用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記錄的方式返回了預期的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.