Skip to content

Commit ce3b139

Browse files
Fix Dropbox sync
In order to support short-lived tokens we have to provide a refresh token, which can be obtained from the Dropbox api. Fixes issue #362
1 parent 7767662 commit ce3b139

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

doc/config/sync/dropbox.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "dropbox",
33
"options": {
4-
"token": "myCrazyLongApiTokenThatIGotFromDropbox",
4+
"refreshToken": "myCrazyLongRefreshTokenThatIGotFromDropbox",
55
"appKey": "myAppKey",
66
"appSecret": "myAppSecret",
77
"path": "/some/dir"

doc/config/sync/dropbox.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<sync type="dropbox">
33
<!-- mandatory -->
4-
<option name="token" value="mycrazylongapitokenthatigotfromdropbox" />
4+
<option name="refreshToken" value="mycrazylongrefreshtokenthatigotfromdropbox" />
55

66
<!-- mandatory -->
77
<option name="appKey" value="myAppKey" />

src/Backup/Sync/Dropbox.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ class Dropbox implements Simulator
3737
*
3838
* @var string
3939
*/
40-
protected $token;
40+
private $appKey;
41+
42+
/**
43+
* @var string
44+
*/
45+
private $appSecret;
46+
47+
/**
48+
* @var string
49+
*/
50+
protected $refreshToken;
4151

4252
/**
4353
* Remote path
@@ -60,16 +70,6 @@ class Dropbox implements Simulator
6070
*/
6171
protected $time;
6272

63-
/**
64-
* @var string
65-
*/
66-
private $appKey;
67-
68-
/**
69-
* @var string
70-
*/
71-
private $appSecret;
72-
7373
/**
7474
* (non-PHPDoc)
7575
*
@@ -85,12 +85,12 @@ public function setup(array $config)
8585
}
8686

8787
// check for mandatory options
88-
$this->validateConfig($config, ['token', 'path', 'appKey', 'appSecret']);
88+
$this->validateConfig($config, ['refreshToken', 'path', 'appKey', 'appSecret']);
8989

90-
$this->time = time();
91-
$this->token = $config['token'];
92-
$this->appKey = $config['appKey'];
93-
$this->appSecret = $config['appSecret'];
90+
$this->time = time();
91+
$this->refreshToken = $config['refreshToken'];
92+
$this->appKey = $config['appKey'];
93+
$this->appSecret = $config['appSecret'];
9494
// make sure the path contains a leading slash
9595
$this->path = new Path(Util\Path::withLeadingSlash($config['path']), $this->time);
9696

@@ -179,7 +179,14 @@ protected function createCollector(Target $target) : Collector
179179
protected function createClient() : DropboxApi\Dropbox
180180
{
181181
if (!$this->client) {
182-
$app = new DropboxApi\DropboxApp($this->appKey, $this->appSecret, $this->token);
182+
$app = new DropboxApi\DropboxApp($this->appKey, $this->appSecret);
183+
$client = new DropboxApi\Dropbox($app);
184+
$authHelper = $client->getAuthHelper();
185+
$token = $authHelper->getRefreshedAccessToken(
186+
new DropboxApi\Models\AccessToken(['refresh_token' => $this->refreshToken])
187+
);
188+
189+
$app = new DropboxApi\DropboxApp($this->appKey, $this->appSecret, $token->getToken());
183190
$this->client = new DropboxApi\Dropbox($app);
184191
}
185192
return $this->client;

0 commit comments

Comments
 (0)