-
I cannot authenticate using my app key and secret, as described in the readme.
I do not get any meaningful exception thrown either. The response coming back:
However, if I pass in a token generated from the Dropbox App Console, then everything works as expected. Any idea what might be happening here? My code is pretty straightforward. I'll note that I have confirmed my environment variables are returning the correct app key and secret strings. <?php
namespace Something\Api\Models;
use Monolog\Logger;
use Spatie\Dropbox\Client;
use Something\Api\Exceptions\ApiException;
/**
* Dropbox model
* https://www.dropbox.com/developers/documentation/http/documentation
*/
class DropboxModel
{
public function __construct(Logger $logger)
{
$this->logger = $logger;
$this->key = DROPBOX_KEY;
$this->secret = DROPBOX_SECRET;
$this->token = DROPBOX_TOKEN;
$this->client = new Client([$this->key, $this->secret]);
//$this->client = new Client($this->token);
}
public function listAssets()
{
try {
$assets = $this->client->listFolder('', true);
return $assets;
} catch (Exception $e) {
$this->logger->error("[{$e->getCode()}] ERROR: {$e->getMessage()}");
throw new ApiException("[{$e->getCode()}] Unable to list assets: {$e->getMessage()}");
}
}
} I'll add that after searching through the Dropbox documentation, the only endpoints that appear to accept App Authentication, are: /app - allows you to test connection and credentials Everything else requires User Authentication (token). I see here that there's a method, getHeadersForCredentials that's using Basic auth where the app key and secret are base64 encoded, as described in the Dropbox docs. However, this authentication method is described as "App Authentication", and referring above, virtually no endpoints support app-based authentication. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I think the documentation should be updated to more accurately describe that this package does not provide authentication. It's misleading. For anyone working on a server-side implementation, what you'll want to do, is obtain a Refresh Token and store that securely as an environment variable on your server. Dropbox Refresh Tokens are long-lived, and do not expire unless explicitly revoked. You should use this Refresh Token to fetch Access Tokens (which are short-lived) as needed. This is the appropriate flow per the Dropbox team as described in their oAuth Guide and by a Dropbox dev in this community thread. To do that, first obtain an Access Code by visiting this URL in your browser. You'll be brought to a Dropbox site to authorize your app and be presented with the access code. Note: it's important you include
Once you've obtained your Access Code, make your first request to obtain an Access Token. I'd recommend just doing this from console as you'll only need to do it once for the purpose of grabbing a Refresh Token.
You should get back a response that looks something like this -- your scopes will of course be based on what you've setup for your app. {
"uid": "xxxxxxxxxx",
"access_token": "xxxxxxxxxx",
"expires_in": 14400,
"token_type": "bearer",
"scope": "files.content.read files.content.write",
"refresh_token": "xxxxxxxxxx",
"account_id": "dbid:xxxxxxxxxx"
} Here, you're looking for the You can then fetch your access token like this: private function getToken()
{
try {
$client = new \GuzzleHttp\Client();
$res = $client->request("POST", "https://{$this->key}:{$this->secret}@api.dropbox.com/oauth2/token", [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => $this->refreshToken,
]
]);
if ($res->getStatusCode() == 200) {
return json_decode($res->getBody(), TRUE);
} else {
return false;
}
}
catch (Exception $e) {
$this->logger->error("[{$e->getCode()}] {$e->getMessage()}");
return false;
}
} Example response: {
"token_type": "bearer",
"access_token": "xxxxxxxxxx",
"expires_in": 14400
} |
Beta Was this translation helpful? Give feedback.
-
I've put together a public GIST, Dropbox API V2: PHP Authentication Process, that describes how to approach the authentication process for a server-side PHP implementation, along with ideas for caching and refreshing the token. |
Beta Was this translation helpful? Give feedback.
I've put together a public GIST, Dropbox API V2: PHP Authentication Process, that describes how to approach the authentication process for a server-side PHP implementation, along with ideas for caching and refreshing the token.