【问题标题】:Guzzle and Form + Basic Auth issues with LaravelLaravel 的 Guzzle 和 Form + 基本身份验证问题
【发布时间】:2017-05-12 22:20:32
【问题描述】:

我一直在研究从 Livecoding.tv 中获取一些数据,我目前正在重用我用于 twitch API 的 Oauth2 控制器,这应该很简单。

如果有人不知道,Oauth 使用的流程如下:

  1. 使用您的应用代码将用户重定向到第三方 Oauth 链接。

  2. 用户授权。

  3. 用户使用授权令牌再次重定向到您的网站,然后您可以将其发布到第三方以获取您的刷新令牌等。

现在在第 3 步,我遇到了一些问题。以下是开发者的描述和示例:

获取令牌:https://www.livecoding.tv/o/token/

标题

HTTP 基本身份验证,使用 application_code 作为用户名,application_secret 作为密码,如下例所示。

POST 正文

code=EXAMPLE Token gotten from redirect
grant_type=Your grant type (authorization_type)
redirect_uri=Your redirect URL

这是来自工作 curl 请求文档的示例 cURL 请求。

curl -X POST -d "grant_type=authorization_code&code=Php4iJpXGpDT8lCqgBcbfQ2yzhB0Av&client_id=vCk6rNsC&redirect_uri=http://localhost/externalapp" -u"vCk6rNsC:sfMxcHUuNnZ" https://www.livecoding.tv/o/token/

所以我尝试在 Postman(https://www.getpostman.com/) 中进行此操作,开箱即用,然后我要求 Postman 将其转换为 PHP,看看我是否遗漏了什么。这是 Postman 工作请求的结果:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.livecoding.tv/o/token/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "code=SOMECODE&client_id=SOMECLIENTID&redirect_uri=SOMEURL&grant_type=authorization_code",
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic U09NRVVTRVI6U09NRVBBU1NXT1JE",
    "cache-control: no-cache",
    "content-type: application/x-www-form-urlencoded",
    "postman-token: c8df4bbc-cbd0-73eb-df35-80210989db33"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

我很高兴这很快就解决了,所以我只需要让 Guzzle 客户端做同样的事情,这已经适用于 Twitch Oauth,这是我的代码:

{
            $providers = ServiceProvider::findOrFail(2);
            $client = new Client([
                'base_uri' => $providers->oauth_url . '/token/',
                'form_params' => [
                    'code' => $token,
                    'grant_type' => 'authorization_code',
                    'client_id' => $providers->application_code,
                    'redirect_uri' => $providers->redirection_url
                ],
                'auth' => [
                    'somestring',
                    'someotherstring',
                ],
                'headers' => [
                    'Content type' => 'application/x-www-form-urlencoded',
                    'cache-control' => 'no-cache'
                ]
            ]);
            $response = $client->request('POST');
            return ($response);
        }

这只会给我一个 401,所以我决定做一些调试,如果我在发送之前停止它,这就是请求:

Client {#637 ▼
  -config: array:10 [▼
    "base_uri" => Uri {#676 ▼
      -scheme: "https"
      -userInfo: ""
      -host: "www.livecoding.tv"
      -port: null
      -path: "/o/token/"
      -query: ""
      -fragment: ""
    }
    "form_params" => array:4 [▼
      "code" => "SOMECODE"
      "grant_type" => "authorization_code"
      "client_id" => "SOMECLIENTID"
      "redirect_uri" => "http://REDIRECTURI"
    ]
    "auth" => array:2 [▼
      0 => "SOMECLIENTID"
      1 => "SOMECLIENTSECRET"
    ]
    "headers" => array:3 [▼
      "Content type" => "application/x-www-form-urlencoded"
      "cache-control" => "no-cache"
      "User-Agent" => "GuzzleHttp/6.2.1 curl/7.26.0 PHP/5.6.27-1~dotdeb+7.1"
    ]
    "handler" => HandlerStack {#664 ▼
      -handler: Closure {#671 ▼
        class: "GuzzleHttp\Handler\Proxy"
        parameters: {▼
          $request: {▼
            typeHint: "Psr\Http\Message\RequestInterface"
          }
          $options: {▼
            typeHint: "array"
          }
        }
        use: {▼
          $default: Closure {#669 ▼
            class: "GuzzleHttp\Handler\Proxy"
            parameters: {▼
              $request: {▼
                typeHint: "Psr\Http\Message\RequestInterface"
              }
              $options: {▼
                typeHint: "array"
              }
            }
            use: {▼
              $default: CurlMultiHandler {#634 ▼
                -factory: CurlFactory {#667 ▼
                  -handles: []
                  -maxHandles: 50
                }
                -selectTimeout: 1
                -active: null
                -handles: []
                -delays: []
              }
              $sync: CurlHandler {#666 ▼
                -factory: CurlFactory {#665 ▼
                  -handles: []
                  -maxHandles: 3
                }
              }
            }
            file: "/LARAVELPATH/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php"
            line: "25 to 29"
          }
          $streaming: StreamHandler {#670 ▼
            -lastHeaders: []
          }
        }
        file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php"
        line: "49 to 53"
      }
      -stack: array:4 [▼
        0 => array:2 [▼
          0 => Closure {#672 ▼
            class: "GuzzleHttp\Middleware"
            parameters: {▶}
            file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Middleware.php"
            line: "54 to 69"
          }
          1 => "http_errors"
        ]
        1 => array:2 [▼
          0 => Closure {#673 ▼
            class: "GuzzleHttp\Middleware"
            parameters: {▶}
            file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Middleware.php"
            line: "148 to 150"
          }
          1 => "allow_redirects"
        ]
        2 => array:2 [▼
          0 => Closure {#674 ▼
            class: "GuzzleHttp\Middleware"
            parameters: {▶}
            file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Middleware.php"
            line: "27 to 43"
          }
          1 => "cookies"
        ]
        3 => array:2 [▼
          0 => Closure {#675 ▼
            class: "GuzzleHttp\Middleware"
            parameters: {▶}
            file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Middleware.php"
            line: "216 to 218"
          }
          1 => "prepare_body"
        ]
      ]
      -cached: null
    }
    "allow_redirects" => array:5 [▼
      "max" => 5
      "protocols" => array:2 [▼
        0 => "http"
        1 => "https"
      ]
      "strict" => false
      "referer" => false
      "track_redirects" => false
    ]
    "http_errors" => true
    "decode_content" => true
    "verify" => true
    "cookies" => false
  ]
}

正如您在此处看到的,这里只有一个名为“auth”的参数,而不是 Guzzle 中记录的基本身份验证:http://docs.guzzlephp.org/en/latest/request-options.html#auth

虽然我会提到输入了正确的值。

对于我刚刚做的dd($client)的调试,不知道这样能不能给我所有的答案?

所以另一种可能性是对其进行 base64 编码(就像它通常在基本身份验证中完成一样),并手动添加“授权”标头,我试过了,但我不知道我这样做时是否正确:

$credentials = base64_encode($clientvariable . ':' . $clientsecretvariable)

那会是正确的方法吗?虽然如果我没有让 auth 参数在 Guzzle 中工作,我宁愿将此作为最后的手段。

我知道有关 Guzzle 和基本身份验证的其他 Stackoverflow 问题,是的,我已经阅读了它们,希望我已经提供了足够的信息来证明这一点。

【问题讨论】:

    标签: php authentication oauth laravel-5.3 guzzle


    【解决方案1】:

    这里的问题与我如何从 Guzzle 返回响应有关。

    由于我刚刚返回了 $response,这只是流,浏览器从未得到任何返回,并且由于 chrome 自动刷新,他运行了两次请求,而第二次,我将只有我的代码两次,这会返回错误。

    我最终为此发疯了,直到我更换了浏览器,发现什么都没发生,这为我指明了正确的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-16
      • 2015-07-22
      • 2016-11-16
      • 1970-01-01
      • 2018-12-20
      • 2021-05-17
      • 2010-11-24
      • 1970-01-01
      相关资源
      最近更新 更多