【问题标题】:Guzzle with Laravel send an application/x-www-form-urlencoded request使用 Laravel 进行 Guzzle 发送 application/x-www-form-urlencoded 请求
【发布时间】:2020-07-21 17:46:10
【问题描述】:

在 Laravel 中使用 Guzzle 发出 POST 请求时,我不断收到此错误:

{
data: {
error: "invalid_client"
}
}

我 100% 确定 ID 和机密是正确的,因为它在 Postman 中工作。这是我的代码。

$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]);

        }

谁能帮帮我?我也试过这个标题:

 'headers' => [
                        'Content-Type' => 'application/x-www-form-urlencoded',
                    ],

这是工作邮递员代码。这工作正常。也许任何人都可以看到 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\": \"test@test.net\",\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\": \"test@test.net\"\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": {}
}

【问题讨论】:

    标签: laravel post postman guzzle


    【解决方案1】:

    如果你尝试 Guzzle 周围的 laravel Facade 会发生什么?

    步骤如下:

    // 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());
    

    【讨论】:

    • 由于某种原因,我无法正常工作。添加使用 Illuminate\Support\Facades\Http 时未定义的类;在控制器中。
    • 但您确实执行了 composer require 命令,对吧?也许你还必须做一个 composer dump && composer install .. 这也是来自 laravel 7 的文档,如果已经成功使用它的话..
    • 是的,我确实运行了 composer require guzzlehttp/guzzle。使用 laravel 5.6
    • 哦,好吧,这行不通。那个 laravel 包装器当时还不存在
    【解决方案2】:

    试试这个,对我来说很好用

    $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());
    
        
    

    【讨论】:

    • 继续获取:客户端错误:POST https:/example.com/connect/token 导致 400 Bad Request 响应:{"error":"invalid_client"}
    • 这是您尝试调用的服务器的响应,我认为您缺少调用所需的身份验证数据,但 guzzle 没有问题
    • 谢谢。我已经添加了工作邮递员代码。你能看出 Guzzle 中可能缺少什么吗?
    • 我相信guzzle没有问题,如果您能提供邮递员的工作数据,我将能够提供帮助。
    • 请检查代码。这是我现在的主要问题。
    【解决方案3】:

    发现问题。愚蠢的错字。在“client_secret”中缺少“=>”。

    【讨论】:

      猜你喜欢
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      相关资源
      最近更新 更多