【问题标题】:how fix Authorization Bearer Problem in guzzle如何解决guzzle中的授权承载问题
【发布时间】:2021-01-16 22:27:20
【问题描述】:

我正在尝试将此请求发送到服务器,但出现 401 错误 问题可能出在代码的哪一部分?

“guzzle 6.3 版”

try {
        $urlDoPayment = 'https://api.example.com/v1/pay';
        $client = new Client();
        try {
            $response = $client->request('POST', $urlDoPayment, [
                \GuzzleHttp\RequestOptions::JSON => [
                    'form_params' => [
                        'amount' => 100,
                        'returnUrl' => "https://example.com/payment/verify",
                        'payerIdentity' => "",
                        'payerName' => "",
                        'description' => "",
                        'clientRefId' => ""
                    ],
                    'headers' => [
                        'Content-Type' => 'application/json',
                        'Authorization' => 'Bearer MY_TOKEN',
                        'Accept' => 'application/json'
                    ]
                ]
            ]);
            $statusCode = $response->getStatusCode();
            $content = $response->getBody();
            dd($content);
        } catch (GuzzleException $e) {
            dd($e->getMessage());
        }

    } catch (\Exception $exception) {
        dd($exception->getCode());
    }

【问题讨论】:

    标签: php laravel curl guzzle


    【解决方案1】:

    请求中的headers 嵌套在请求选项的错误部分。如果令牌有效,那至少应该修复 401 错误。

    试试:

    $response = $client->request('POST', $urlDoPayment, [
        \GuzzleHttp\RequestOptions::JSON => [
            'form_params' => [
                'amount' => 100,
                'returnUrl' => "https://example.com/payment/verify",
                'payerIdentity' => "",
                'payerName' => "",
                'description' => "",
                'clientRefId' => ""
            ],
            // headers should not be here
        ], // json post body params end
        'headers' => [
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer MY_TOKEN',
            'Accept' => 'application/json'
        ]
    ]);
    

    【讨论】:

      【解决方案2】:

      使用这段代码解决了这个问题

      $response = $client->request('POST', $urlDoPayment, [
                      'json' => [
                          'amount' => 100,
                          'returnUrl' => "http://example.com/payment/verify",
                          'payerIdentity' => "",
                          'payerName' => "",
                          'description' => "",
                          'clientRefId' => ""
                      ],
                      'headers' => [
                          'Content-Type' => 'application/json',
                          'Authorization' => 'Bearer MY_TOKEN',
                          'Accept' => 'application/json'
                      ]
                  ]);
      

      【讨论】:

        【解决方案3】:

        我怀疑问题出在未定义的端口或协议上。标题内容对我来说不是问题。我通过更改解决了同样的问题:

        115.12.3.44115.12.3.44:80

        也测试过

        115.12.3.44http://115.12.3.44

        google.comhttp://google.com

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-19
          • 1970-01-01
          • 2020-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-05
          • 2011-07-04
          相关资源
          最近更新 更多