Reputation: 1
I keep getting this error when doing a POST request with Guzzle in Laravel:
{
data: {
error: "invalid_client"
}
}
I am 100% sure the ID's and secrets are correct, because it's working in Postman. This is my code.
$client = new GuzzleHttp\Client();
try {
$res = $client->request('POST', 'https://example.com', [
'form_params' => [
'grant_type' => '*',
'client_id' => '*',
'client_secret', '*',
'scope' => '*',
'connector' => '*',
'api_token' => '*',
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd($res);
} catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
Can anyone please help me out? I also tried with this header:
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
This is the working Postman code. This is working fine. Maybe anyone can see what might be missing in the Guzzle?
{
"info": {
"_postman_id": "*",
"name": "Test",
"schema": "*"
},
"item": [
{
"name": "Add Outbound Invoice",
"request": {
"method": "POST",
"header": [
{
"key": "Authorization",
"value": "Bearer [ACCESSTOKEN]",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"number\": \"test1\",\r\n \"subject\": \"Test\",\r\n \"paymentMethod\": \"Online\",\r\n \"paymentID\": \"onlinePaymentId\",\r\n \"date\": \"2020-02-13T15:57:41.342Z\",\r\n \"dueDate\": \"2020-02-13T15:57:41.342Z\",\r\n \"currency\": \"EUR\",\r\n \"remarks\": \"Test comments\",\r\n \"customer\": {\r\n \"identifier\": \"[email protected]\",\r\n \"name\": \"George W. Bush\",\r\n \"addressLine_1\": \"White House\",\r\n \"zipCode\": \"54623\",\r\n \"city\": \"Washington\",\r\n \"countryCode\": \"US\",\r\n \"emailAddress\": \"[email protected]\"\r\n },\r\n \"lines\": [\r\n {\r\n \"description\": \"Test Item 1\",\r\n \"quantity\": 1,\r\n \"lineAmount\": 10,\r\n \"lineVATAmount\": 2.1,\r\n \"product\": {\r\n \"identifier\": \"testItem1\",\r\n \"description\": \"This is a test item\",\r\n \"salesPrice\": 20,\r\n \"vatType\": \"High\",\r\n \"vatPercentage\": 21,\r\n \"glAccountCode\": \"80000\"\r\n }\r\n }\r\n ]\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "example.com",
"protocol": "https",
"host": [
"api",
"yukiconnector",
"nl"
],
"path": [
"api",
"SalesInvoice"
]
}
},
"response": []
},
{
"name": "Get Token",
"protocolProfileBehavior": {
"disabledSystemHeaders": {
"user-agent": true,
"accept-encoding": true,
"connection": true
}
},
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "*",
"type": "text"
},
{
"key": "client_id",
"value": "*",
"type": "text"
},
{
"key": "client_secret",
"value": "*",
"type": "text"
},
{
"key": "scope",
"value": "*",
"type": "text"
},
{
"key": "connector",
"value": "*",
"type": "text"
},
{
"key": "api_token",
"value": "*",
"type": "text"
}
]
},
"url": {
"raw": " https://example.com",
"protocol": "https",
"host": [
"auth",
"yukiconnector",
"nl"
],
"path": [
"identity",
"connect",
"token"
]
}
},
"response": []
}
],
"protocolProfileBehavior": {}
}
Upvotes: 0
Views: 5314
Reputation: 391
Try This, It is working fine with me
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://example.com', [
'headers' => ['Content-type: application/x-www-form-urlencoded'],
'form_params' => [
'grant_type' => '*',
'client_id' => '*',
'client_secret' => '*',
'scope' => '*',
'connector' => '*',
'api_token' => '*',
],
'timeout' => 20, // Response timeout
'connect_timeout' => 20, // Connection timeout
]);
dd($response->getBody()->getContents());
Upvotes: 1
Reputation: 1
Found the problem. Stupid typo. Was missing the '=>' at the 'client_secret'.
Upvotes: 0
Reputation: 1732
What happens if you try the laravel Facade around Guzzle?
The steps are as follows:
// make sure the composer component is installed in console
composer require guzzlehttp/guzzle
// don't forget include at top of Controller
use Illuminate\Support\Facades\Http;
// inside function
$response = Http::post('http://test.com/users', [
'name' => 'Steve',
'role' => 'Network Administrator',
]);
dd($response->json());
Upvotes: 0