138 lines
4.6 KiB
PHP
138 lines
4.6 KiB
PHP
<?php
|
|
|
|
class GHLOAuth2 {
|
|
private $config;
|
|
private $baseUrl = 'https://services.leadconnectorhq.com';
|
|
private $authorizeUrl = 'https://marketplace.gohighlevel.com/oauth/chooselocation';
|
|
private $scopes = ['calendars.readonly', 'calendars/events.readonly'];
|
|
private $redirectUri = 'http://localhost:9000/ghl/callback';
|
|
private $clientId = 'xxx';
|
|
private $clientSecret = 'xxx';
|
|
|
|
public function __construct(array $config) {
|
|
if (!isset($config['gohighlevel_client_id']) || !isset($config['gohighlevel_client_secret'])) {
|
|
throw new Exception('Client ID and Client Secret are required.');
|
|
}
|
|
$this->config = $config;
|
|
}
|
|
|
|
public function getAuthorizationUrl() {
|
|
$params = [
|
|
'response_type' => 'code',
|
|
'redirect_uri' => $this->config['gohighlevel_redirect_uri'] ?? $this->redirectUri,
|
|
'client_id' => $this->config['gohighlevel_client_id'] ?? $this->clientId,
|
|
'scope' => implode(' ', $this->scopes),
|
|
'user_type' => 'Location'
|
|
];
|
|
|
|
return $this->authorizeUrl . '?' . http_build_query($params);
|
|
}
|
|
|
|
/*
|
|
Gohighlevel returns the code in the redirect uri ?code=xxxx
|
|
*/
|
|
|
|
public function getAccessToken($code, $redirectUri = null) {
|
|
$data = [
|
|
'client_id' => $this->config['gohighlevel_client_id'] ?? $this->clientId,
|
|
'client_secret' => $this->config['gohighlevel_client_secret'] ?? $this->clientSecret,
|
|
'grant_type' => 'authorization_code',
|
|
'code' => $code,
|
|
'user_type' => 'Location'
|
|
];
|
|
|
|
if ($redirectUri) {
|
|
$data['redirect_uri'] = $redirectUri;
|
|
}
|
|
|
|
return $this->makeTokenRequest($data);
|
|
}
|
|
|
|
public function refreshToken($refreshToken) {
|
|
if (empty($refreshToken)) {
|
|
throw new Exception('Refresh token is required');
|
|
}
|
|
|
|
|
|
$data = [
|
|
'client_id' => $this->config['gohighlevel_client_id'] ?? $this->clientId,
|
|
'client_secret' => $this->config['gohighlevel_client_secret'] ?? $this->clientSecret,
|
|
'grant_type' => 'refresh_token',
|
|
'refresh_token' => $refreshToken,
|
|
'user_type' => 'Location'
|
|
];
|
|
|
|
return $this->makeTokenRequest($data);
|
|
}
|
|
|
|
private function makeTokenRequest($data) {
|
|
$curl = curl_init();
|
|
|
|
$options = [
|
|
CURLOPT_URL => $this->baseUrl . '/oauth/token',
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => '',
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => 'POST',
|
|
CURLOPT_POSTFIELDS => http_build_query($data),
|
|
CURLOPT_HTTPHEADER => [
|
|
'Accept: application/json',
|
|
'Content-Type: application/x-www-form-urlencoded'
|
|
],
|
|
];
|
|
|
|
curl_setopt_array($curl, $options);
|
|
|
|
$response = curl_exec($curl);
|
|
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
if ($error) {
|
|
return [
|
|
'success' => false,
|
|
'error' => 'Curl error: ' . $error
|
|
];
|
|
}
|
|
|
|
$responseData = json_decode($response, true);
|
|
if ($responseData === null) {
|
|
return [
|
|
'success' => false,
|
|
'error' => 'Error decoding JSON response'
|
|
];
|
|
}
|
|
|
|
|
|
|
|
if ($httpCode !== 200) {
|
|
return [
|
|
'success' => false,
|
|
'error' => $responseData['message'] ?? 'Unknown error occurred',
|
|
'code' => $httpCode,
|
|
'raw_response' => $responseData
|
|
];
|
|
}
|
|
|
|
// Ensure we always return refresh_token if it exists in the response
|
|
return [
|
|
'success' => true,
|
|
'data' => [
|
|
'access_token' => $responseData['access_token'] ?? null,
|
|
'refresh_token' => $responseData['refresh_token'] ?? null,
|
|
'expires_in' => $responseData['expires_in'] ?? null,
|
|
'locationId' => $responseData['locationId'] ?? null,
|
|
'companyId' => $responseData['companyId'] ?? null,
|
|
'userId' => $responseData['userId'] ?? null,
|
|
'scope' => $responseData['scope'] ?? null,
|
|
'token_type' => $responseData['token_type'] ?? null
|
|
],
|
|
'raw_response' => $responseData
|
|
];
|
|
}
|
|
}
|