Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Change authToken to Oauth #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 21 additions & 46 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
{
$this->setRequestOauth($acessToken);

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->httpClient = $httpClient ?: new BaseClient($requestOptions);
$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));
}
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}";
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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=(?<token>[a-z0-9]+)/', (string) $response->getBody(), $matches)) {
$authToken = $matches['token'];
}

return $authToken;
}
}