问题
this is my code
$data = array(
'grant_type' => 'Password',
'username' => 'username',
'password' => 'pwd',
'DeviceId' => ''
);
$url = "http://test/sync/oauth/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
print_r($response);`
I think is correct, but the server return me this error
Error: call to URL http://test/sync/oauth/token failed with status 400, response {"error":"unsupported_grant_type"}, curl_error , curl_errno 0
Where is the error?
回答1:
The Default implementation of OAuthAuthorizationServerHandler only accepts form encoding (i.e. application/x-www-form-urlencoded) and not JSON encoding (application/JSON)
your request's ContentType should be application/x-www-form-urlencoded and pass the data in the body as: grant_type=password&username=username&password=password i.e. not in JSON format.
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
回答2:
For those who need it... This is the solution:
$data = array(
'grant_type' => 'password',
'username' => 'username',
'password' => 'password',
'DeviceId' => ''
);
$url_token = "http://test/sync/oauth/token";
try {
$oauth = new OAuth('username','password');
$signature = $oauth->fetch ($url_token,$data,'POST');
$response_info = $oauth->getLastResponseInfo();
header("Content-Type: {$response_info["content_type"]}");
$token = $oauth->getLastResponse();
$token = json_decode($token, true);
$authorization_token = $token['access_token'];
} catch(OAuthException $E) {
echo "Response: ". $E->lastResponse . "\n";
}
return $authorization_token;
回答3:
Ok so after wasting my couple of hours and doing tons of testing I came up with a solution and hope it helps with your case
Tip : Dont Concatenate the Post Arguments into a string and add it in Array just use it sepratlely so with that you dont even need to add the Custom_HEADERS => Content-Type : application/x-www-form-urlencoded
Some people says that you need to add application/x-www-form-urlencoded as Content-Type then it would work but my solution works perfectly heres the working code
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, 'YOUR_TOKEN_URL_HERE');
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'Username' => 'YOUR_USERNAME',
'Password' => 'YOUR_PASSWORD',
'grant_type' => 'password'
)));
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo $result;
This code works perfectly for Laravel Bearer Token and others too
Reference : CURL POST FORMAT
Hope my answer help in your Situation Happy Coding :) !
来源:https://stackoverflow.com/questions/38452822/php-post-request-and-unsupported-grant-type