protected function getClient() { if (null === $this->client) { $this->client = new \Google_Client(); $this->client->setAuthConfig($this->credentials); $this->client->setApplicationName($this->name); foreach ($this->services as $serviceName => $service) { if (!isset($service['class'])) { throw new InvalidConfigException("Not set service class for service \"{$serviceName}\""); } $service['scopes'] = (array) $service['scopes']; if (empty($service['scopes'])) { throw new InvalidConfigException("Not set scopes for service \"{$serviceName}\""); } $this->client->addScope($service["scopes"]); } $scopes = $this->client->getScopes(); if (empty($scopes)) { throw new InvalidConfigException("Not set scopes for application \"{$this->name}\""); } } return $this->client; }
public function connectDrive() { $this->autoRender = false; $this->loadModel('Tokens'); // GAUTH: connect drive $client = new \Google_Client(); $client->setAuthConfigFile('../client_secret.json'); $client->addScope(\Google_Service_Drive::DRIVE); $client->authenticate($this->request->query['code']); $drive_token = $client->getAccessToken(); $this->request->session()->write('user.drive_token', $drive_token); $data = ['user_id' => $this->request->session()->read('user.id'), 'scope' => implode(" ", $client->getScopes()), 'type' => 'refresh', 'token' => json_decode($drive_token)->refresh_token]; $newToken = $this->Tokens->newEntity($data); $this->Tokens->save($newToken); return $this->redirect(['controller' => 'Player']); }