modified 修复AccessToken的获取方式

This commit is contained in:
zhaoxiang 2021-11-16 19:33:15 +08:00
parent 4167c6ff6b
commit 68baf960b3
2 changed files with 14 additions and 3 deletions

View File

@ -32,6 +32,13 @@ class BaseClient {
*/
protected $baseUri;
/**
* @var bool
*/
protected $postAccessToken = true;
private $accessToken;
/**
* BaseClient constructor.
* @param ServiceContainer $app
@ -146,6 +153,9 @@ class BaseClient {
if (empty($this->middlewares)) {
$this->registerHttpMiddlewares();
}
if ($this->postAccessToken && isset($options['json']) && is_array($options['json'])) {
$options['json'][$this->accessToken->getTokenKey()] = $this->accessToken->getToken()[$this->accessToken->getTokenKey()];
}
$response = $this->performRequest($url, $method, $options);
return $this->castResponseToType($response, $this->app->config->get('response_type'));
@ -170,8 +180,10 @@ class BaseClient {
protected function registerHttpMiddlewares(): void {
// retry
$this->pushMiddleware($this->retryMiddleware(), 'retry');
// access token
$this->pushMiddleware($this->accessTokenMiddleware(), 'access_token');
// access token 在url上添加accessToken的参数
if (!$this->postAccessToken) {
$this->pushMiddleware($this->accessTokenMiddleware(), 'access_token');
}
}
/**

View File

@ -190,7 +190,6 @@ trait HasHttpRequests {
protected function fixJsonIssue(array $options): array {
if (isset($options['json']) && is_array($options['json'])) {
$options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/json']);
$options['json'][$this->accessToken->getTokenKey()] = $this->accessToken->getToken()[$this->accessToken->getTokenKey()];
if (empty($options['json'])) {
$options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_FORCE_OBJECT);
} else {