diff --git a/src/Box.php b/src/Box.php index 845ebea..b9baa02 100644 --- a/src/Box.php +++ b/src/Box.php @@ -2,10 +2,10 @@ namespace PhpBox; use PhpBox\Config\Config; -use PhpBox\Items\{Folder}; +use PhpBox\Items\{Item, Folder}; class Box { - private $baseUrl = "https://api.box.com/2.0/"; + const baseUrl = "https://api.box.com/2.0/"; private $AccessToken; private $config; @@ -37,6 +37,29 @@ public function requestAccessToken() { return false; } + public function requestExchangeToken($scopes = ["base_preview", "item_download"], $folder = NULL, $token = NULL) { + if($token == NULL) { + $token = $this->getValidAccessToken(); + } + $client = new \GuzzleHttp\Client(); + $params = [ + 'subject_token' => (string)$token, + 'subject_token_type' => 'urn:ietf:params:oauth:token-type:access_token', + 'grant_type' => 'urn:ietf:params:oauth:grant-type:token-exchange', + 'scope' => implode(" ", $scopes) + ]; + if ($folder != NULL) { + if ($folder instanceof Item && $folder->isFolder()) { + $folder = (string)($folder->getId()); + } + $params['resource'] = Folder::endpointUrl.$folder; + } + $response = $client->request('POST', $this->config->getAuthenticationUrl(), [ + 'form_params' => $params + ]); + return new Token($response->getBody()->getContents()); + } + public function getAccessToken() { return $this->AccessToken; } @@ -55,7 +78,7 @@ public function requestFolder($id = "0", $fields = []) { if(!empty($fields)) { $query['fields'] = implode(",", $fields); } - $response = $client->request('GET', $this->baseUrl."folders/$id", [ + $response = $client->request('GET', self::baseUrl."folders/$id", [ 'headers' => $headers, 'query' => $query ]); diff --git a/src/Items/Folder.php b/src/Items/Folder.php index 0c04a4f..973bc74 100644 --- a/src/Items/Folder.php +++ b/src/Items/Folder.php @@ -1,9 +1,11 @@ id = $data->id; } + public function getId() { + return $this->id; + } + + public function getType() { + return $this->type; + } + public function isFolder() { return $this->type == 'folder'; }