Skip to content

Commit 390f463

Browse files
committed
Ver.2 Token service class is implemented.
1 parent 4cb52ab commit 390f463

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "msyk/dropbox-api-shortlivedtoken",
3-
"version": "1",
3+
"version": "2",
44
"time": "2022-07-07",
55
"repositories": [
66
{
@@ -42,5 +42,10 @@
4242
"clear": [
4343
"rm -rf node_modules vendor"
4444
]
45+
},
46+
"autoload": {
47+
"psr-4": {
48+
"msyk\\DropboxAPIShortLivedToken\\": "src/"
49+
}
4550
}
4651
}

index.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
require_once __DIR__ . '/vendor/autoload.php';
66

7+
use msyk\DropboxAPIShortLivedToken\AutoRefreshingDropBoxTokenService;
8+
79
// Check the existence of the credentials.php file, and include it.
810
$credFile = __DIR__ . '/credentials.php';
911
if (!file_exists($credFile)) {
@@ -16,25 +18,9 @@
1618
use Spatie\Dropbox\Client;
1719

1820
try {
19-
// Get the access token from refresh token. Referred from https://www.softel.co.jp/blogs/tech/archives/7067
20-
$data = http_build_query(['grant_type' => 'refresh_token', 'refresh_token' => $refreshToken,], '', '&');
21-
$options = [
22-
'http' => [
23-
'ignore_errors' => true, 'method' => 'POST',
24-
'header' => [
25-
'Content-type: application/x-www-form-urlencoded',
26-
'Authorization: Basic ' . base64_encode($appKey . ':' . $appSecret),
27-
],
28-
'content' => $data,
29-
],
30-
];
31-
$context = stream_context_create($options);
32-
$response = json_decode(file_get_contents('https://api.dropbox.com/oauth2/token', false, $context));
33-
$accessKey = $response->access_token;
34-
////////////////////////////////////////////////
35-
21+
$tokenProvider = new AutoRefreshingDropBoxTokenService($refreshToken, $appKey, $appSecret);
3622
// List the items on the root of your dropbox with spatie/dropbox-api
37-
$client = new Client($accessKey);
23+
$client = new Client($tokenProvider);
3824
$list = $client->listFolder("/");
3925
} catch (\Exception $ex) {
4026
var_export($ex->getMessage()); // Very simple output.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace msyk\DropboxAPIShortLivedToken;
4+
5+
use Spatie\Dropbox\TokenProvider;
6+
7+
class AutoRefreshingDropBoxTokenService implements TokenProvider
8+
{
9+
private $refreshToken;
10+
private $appKey;
11+
private $appSecret;
12+
13+
public function __construct($refreshToken, $appKey, $appSecret)
14+
{
15+
$this->refreshToken = $refreshToken;
16+
$this->appKey = $appKey;
17+
$this->appSecret = $appSecret;
18+
}
19+
20+
public function getToken(): string
21+
{
22+
// Get the access token from refresh token. Referred from https://www.softel.co.jp/blogs/tech/archives/7067
23+
$data = http_build_query(['grant_type' => 'refresh_token', 'refresh_token' => $this->refreshToken,], '', '&');
24+
$options = [
25+
'http' => [
26+
'ignore_errors' => true, 'method' => 'POST',
27+
'header' => [
28+
'Content-type: application/x-www-form-urlencoded',
29+
'Authorization: Basic ' . base64_encode($this->appKey . ':' . $this->appSecret),
30+
],
31+
'content' => $data,
32+
],
33+
];
34+
$context = stream_context_create($options);
35+
$response = json_decode(file_get_contents('https://api.dropbox.com/oauth2/token', false, $context));
36+
return $response->access_token;
37+
}
38+
}

0 commit comments

Comments
 (0)