60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MySocket;
|
|
|
|
class GoogleLibrary extends WebLibrary
|
|
{
|
|
private $_client = null;
|
|
private $_session = null;
|
|
private $_access_code = "";
|
|
public function __construct(string $host)
|
|
{
|
|
parent::__construct($host);
|
|
$this->_session = \Config\Services::session();
|
|
}
|
|
|
|
public function getClient()
|
|
{
|
|
if (is_null($this->_client)) {
|
|
// $this->_client = new \Google_Client();
|
|
$this->_client = new \GuzzleHttp\Client();
|
|
$this->_client->setClientId(getenv("socket.google.client.id"));
|
|
$this->_client->setClientSecret(getenv("socket.google.client.key"));
|
|
$this->_client->setRedirectUri(base_url() . getenv("socket.google.client.callback_url"));
|
|
$this->_client->addScope('email');
|
|
$this->_client->addScope('profile');
|
|
}
|
|
return $this->_client;
|
|
}
|
|
|
|
public function getAccessCode(): string
|
|
{
|
|
if ($this->_access_code === "") {
|
|
throw new \Exception("access_code가 지정되지 않았습니다.");
|
|
}
|
|
return $this->_access_code;
|
|
}
|
|
public function setAccessCode(string $access_code)
|
|
{
|
|
$this->_access_code = $access_code;
|
|
}
|
|
|
|
public function setAccessToken()
|
|
{
|
|
//2.토큰정보 가져오기
|
|
$tokenInfo = $this->getClient()->fetchAccessTokenWithAuthCode($this->getAccessCode());
|
|
if (isset($tokenInfo['error'])) {
|
|
throw new \Exception($tokenInfo['error']);
|
|
}
|
|
$token = $tokenInfo[getenv("socket.google.client.token_name")];
|
|
//3. Google Service에 접근하기위해 Access Token을 설정
|
|
$this->getClient()->setAccessToken($token);
|
|
//4. Google에 로그인이 했으므로 세션에 Token값 설정
|
|
$this->_session->set(getenv("socket.google.client.token_name"), $token);
|
|
}
|
|
public function getAccessToken(): ?string
|
|
{
|
|
return $this->_session->get(getenv("socket.google.client.token_name"));
|
|
}
|
|
}
|