From 0e923c50e3b17dbc5b29d28f1640cc407c82854a Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim <0ahmedibrahem@gmail.com> Date: Fri, 22 Oct 2021 14:38:43 +0200 Subject: [PATCH 1/2] Change authToken to Oauth --- src/Client.php | 67 ++++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 46 deletions(-) diff --git a/src/Client.php b/src/Client.php index 6bdef3c..672b3ee 100644 --- a/src/Client.php +++ b/src/Client.php @@ -18,36 +18,46 @@ class Client /** * @var string */ - protected $authToken; + protected $acessToken; /** * @var array|string[][] */ protected $lastResponseHeaders = []; + + protected $requestOptions = []; + /** * Client constructor. * - * @param string $authToken - * @param string|null $email - * @param string|null $password + * @param string $acessToken * @param ClientInterface|null $httpClient * @param array $requestOptions */ - public function __construct($authToken, $email = null, $password = null, ClientInterface $httpClient = null, array $requestOptions = []) + public function __construct($acessToken, ClientInterface $httpClient = null, array $requestOptions = []) { - if ($httpClient && $requestOptions) { + $this->setRequestOauth($acessToken); + + if ($httpClient && $this->requestOptions) { throw new \InvalidArgumentException('If argument 4 is provided, argument 5 must be omitted or passed with an empty array as value'); } $requestOptions += ['base_uri' => self::ENDPOINT, RequestOptions::HTTP_ERRORS => false]; - $this->httpClient = $httpClient ?: new BaseClient($requestOptions); + $this->httpClient = $httpClient ?: new BaseClient($this->requestOptions); if (false !== $this->httpClient->getConfig(RequestOptions::HTTP_ERRORS)) { throw new \InvalidArgumentException(sprintf('Request option "%s" must be set to `false` at HTTP client', RequestOptions::HTTP_ERRORS)); } - if (!$authToken) { - $authToken = $this->auth($email, $password); - } - $this->authToken = $authToken; + } + + /** + * append access token to request header + * + * @param accessToken string + * @return void + */ + private function setRequestOauth($acessToken) + { + $this->requestOptions['headers']['Authorization'] = "Zoho-oauthtoken {$acessToken}; } /** @@ -141,7 +151,6 @@ public function delete($url, $organizationId, $id) protected function getParams($organizationId, array $data = []) { $params = [ - 'authtoken' => $this->authToken, 'organization_id' => $organizationId, ]; if ($data) { @@ -177,38 +186,4 @@ protected function processResult(ResponseInterface $response) } throw new Exception('Response from Zoho is not success. Message: '.$result['message']); } - - /** - * @param string|null $email - * @param string|null $password - * - * @throws Exception - * - * @return string - */ - private function auth($email, $password) - { - if (null === $email || null === $password) { - throw new Exception('Please provide authToken OR Email & Password for auto authentication.'); - } - $response = $this->httpClient->post( - 'https://accounts.zoho.com/apiauthtoken/nb/create', - [ - 'form_params' => [ - 'SCOPE' => 'ZohoBooks/booksapi', - 'EMAIL_ID' => $email, - 'PASSWORD' => $password, - ], - ] - ); - - $this->lastResponseHeaders = $response->getHeaders(); - - $authToken = ''; - if (preg_match('/AUTHTOKEN=(?[a-z0-9]+)/', (string) $response->getBody(), $matches)) { - $authToken = $matches['token']; - } - - return $authToken; - } } From a2aa574a9a3740ae01b3d892b9e2d391e187717b Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim <0ahmedibrahem@gmail.com> Date: Fri, 22 Oct 2021 15:26:02 +0200 Subject: [PATCH 2/2] fix request options --- src/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 672b3ee..581a95e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -39,10 +39,10 @@ public function __construct($acessToken, ClientInterface $httpClient = null, arr { $this->setRequestOauth($acessToken); - if ($httpClient && $this->requestOptions) { + if ($httpClient && $requestOptions) { throw new \InvalidArgumentException('If argument 4 is provided, argument 5 must be omitted or passed with an empty array as value'); } - $requestOptions += ['base_uri' => self::ENDPOINT, RequestOptions::HTTP_ERRORS => false]; + $this->requestOptions += ['base_uri' => self::ENDPOINT, RequestOptions::HTTP_ERRORS => false]; $this->httpClient = $httpClient ?: new BaseClient($this->requestOptions); if (false !== $this->httpClient->getConfig(RequestOptions::HTTP_ERRORS)) { throw new \InvalidArgumentException(sprintf('Request option "%s" must be set to `false` at HTTP client', RequestOptions::HTTP_ERRORS)); @@ -57,7 +57,7 @@ public function __construct($acessToken, ClientInterface $httpClient = null, arr */ private function setRequestOauth($acessToken) { - $this->requestOptions['headers']['Authorization'] = "Zoho-oauthtoken {$acessToken}; + $this->requestOptions['headers']['Authorization'] = "Zoho-oauthtoken {$acessToken}"; } /**