source of /modules/HarborAndSprout/Helpers/GoogleDriveApi.php<?php namespace SproutModules\Karmabunny\HarborAndSprout\Helpers; use Exception; use Kohana; use Sprout\Helpers\HttpReq; use Sprout\Helpers\Sprout; use Sprout\Helpers\Url; class GoogleDriveApi { public static $domain = 'https://www.googleapis.com/drive/v3'; /** * Loads Oauth2 config * * @return array */ public static function getConfig() { return $config['web']; } /** * Request an Oauth2 code * * @return void Redirects */ public static function requestOauthCode() { $config = self::getConfig(); 'response_type' => 'code', 'client_id' => $config['client_id'], 'redirect_uri' => Sprout::absRoot() . 'google-drive/auth-action', 'scope' => 'https://www.googleapis.com/auth/drive', 'access_type' => 'offline', 'approval_prompt' => 'force', ); } /** * Save Oauth token * * @param string $code oAuth2 code * @return array */ public static function saveOauthToken($code) { $config = self::getConfig(); $errors = []; 'code' => $code, 'client_id' => $config['client_id'], 'client_secret' => $config['client_secret'], 'redirect_uri' => Sprout::absRoot() . 'google-drive/auth-action', 'grant_type' => 'authorization_code', ); $resp = json_decode(HttpReq ::post($config['token_uri'], $params), true); if (!empty($resp['error_description'])) throw new Exception ($resp['error_description']); if (empty($resp['access_token'])) $errors[] = 'Did not receive "access_token" field'; if (empty($resp['refresh_token'])) $errors[] = 'Did not receive "refresh_token" field'; 'access_token' => $resp['access_token'], 'refresh_token' => $resp['refresh_token'], 'expires' => time() + $resp['expires_in'], ); return $token_info; } /** * * @param mixed $token * @return void */ public static function refreshOauthToken($token) { if (empty($token)) throw new Exception ('Need to refresh access token but "refresh_token" field empty'); $config = self::getConfig(); 'refresh_token' => $token, 'client_id' => $config['client_id'], 'client_secret' => $config['client_secret'], 'grant_type' => 'refresh_token', ); $resp = json_decode(HttpReq ::post($config['token_uri'], $params), true); if (empty($resp['access_token'])) throw new Exception ('Failed to get new access_token from refresh_token'); 'access_token' => $resp['access_token'], 'refresh_token' => $token, 'expires' => time() + $resp['expires_in'], ); return $token_info; } /** * Load Oauth2 token. May cause a token refresh * * @return mixed * @throws Exception */ public static function getToken() { if (empty($token_info)) return null; if ($token_info['expires'] <= time()) $token_info = self::refreshOauthToken($token_info['refresh_token']); return $token_info['access_token']; } /** * Performs an API request * * @param string $method GET|POST * @param string $url API endpoint URL. @see https://developers.google.com/drive/v3/reference/ * @param array $data Message body. Will be json encoded * @param bool $headers False by default * @return array Response from Google */ public static function apiRequest($method, $url, $data = null, $headers = false) { $token = self::getToken(); if (!in_array($method, ['GET','POST'])) throw new Exception ("Either GET or POST as method"); if (empty($token)) throw new Exception ("API access token missing from cache, please rerun the 'Connect to Google Drive' tool"); $opts = [ 'method' => $method, 'headers' => [ 'Authorization' => "Bearer {$token}", ] ]; if (!empty($headers)) $opts['getheaders'] = true; // Data as request body as JSON if (is_array($data) and $method == 'POST') { $opts['headers']['Content-type'] = 'application/json'; } // Data as request URL params else if (is_array($data) and $method == 'GET') { } $resp = json_decode(HttpReq ::req($url, $opts, $method == 'POST' ? $data : null), true); $resp['headers'] = HttpReq::getLastreqHeaders(); $resp['status'] = HttpReq::getLastreqStatus(); $resp['info'] = HttpReq::getLastreqInfo(); $resp['url'] = $url; return $resp; } }
|