Skip to content

Commit 6ed032c

Browse files
committed
authorization
1 parent a27c593 commit 6ed032c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

lib/Authorization.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace DocPlanner\Client;
4+
5+
use Exception;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\ClientInterface;
8+
use GuzzleHttp\Psr7\MultipartStream;
9+
use GuzzleHttp\Psr7\Request;
10+
11+
class Authorization
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $authUrl;
17+
18+
/**
19+
* @var ClientInterface
20+
*/
21+
private $client;
22+
23+
public function __construct($authUrl, ClientInterface $client = null)
24+
{
25+
$this->authUrl = $authUrl;
26+
$this->client = $client ?: new Client();
27+
}
28+
29+
/**
30+
* @param string $clientId
31+
* @param string $clientSecret
32+
*
33+
* @return string
34+
* @throws ApiException
35+
*/
36+
public function getAccessToken($clientId, $clientSecret)
37+
{
38+
$authForm = [
39+
'grant_type' => 'client_credentials',
40+
'scope' => 'integration',
41+
'client_id' => $clientId,
42+
'client_secret' => $clientSecret
43+
];
44+
45+
foreach ($authForm as $formParamName => $formParamValue) {
46+
$multipartContents[] = [
47+
'name' => $formParamName,
48+
'contents' => $formParamValue
49+
];
50+
}
51+
$authBody = new MultipartStream($multipartContents);
52+
53+
$authRequest = new Request(
54+
'POST',
55+
$this->authUrl,
56+
[],
57+
$authBody
58+
);
59+
60+
try {
61+
$authResponse = $this->client->send($authRequest);
62+
} catch (Exception $e) {
63+
throw new ApiException($e->getMessage(), $e->getCode());
64+
}
65+
$content = json_decode($authResponse->getBody()->getContents(), true);
66+
67+
return $content['access_token'];
68+
}
69+
}

0 commit comments

Comments
 (0)