cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Find out how Tiffany, a Customer Experience superstar uses Dropbox to keep her family in the loop when it comes to her new baby here!

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Cron job to upload files from server to Dropbox

Cron job to upload files from server to Dropbox

jordonshaw
Helpful | Level 5

I've been able to create a PHP app that allows me to upload files through the API into dropbox.  It works great; however, the problem is, the first time you access it, you have to authenticate to get the token and then it's able to work.  This all requires interaction of a user to do so.  My goal is to create a cron job that will run hourly and copy any new files from a folder into my dropbox in order to have a backup copy of the files.  Is this possible?  If so, can anyone point me in the direction of an example?

Thank you!

1 Accepted Solution

Accepted Solutions

Re: Cron job to upload files from server to Dropbox

Greg-DB
Dropboxer

The token you get back after authorizing the app is called an "access token", and is specific to that particular app-user pair. Access tokens for your app don't expire by default, so once the app receives one, you can have it store and re-use it, so it can make API calls in the future for that user without further manual user intervention. 

So, you'll probably want to have your app do something like this each time it runs:

  • check if it has an access token stored already
    • if it does, use that existing access token
    • if it doesn't, send the user through the app authorization flow to get an access token, and store the resulting access token
  • call the API to upload the file(s)

I don't have a sample of this to share in PHP though. Exactly what mechanism you use to store the access token will depend on what platform and libraries you're using. Keep in mind that the access token does enable access to the account, so you should store it securely.

View solution in original post

4 Replies 4

Re: Cron job to upload files from server to Dropbox

Greg-DB
Dropboxer

The token you get back after authorizing the app is called an "access token", and is specific to that particular app-user pair. Access tokens for your app don't expire by default, so once the app receives one, you can have it store and re-use it, so it can make API calls in the future for that user without further manual user intervention. 

So, you'll probably want to have your app do something like this each time it runs:

  • check if it has an access token stored already
    • if it does, use that existing access token
    • if it doesn't, send the user through the app authorization flow to get an access token, and store the resulting access token
  • call the API to upload the file(s)

I don't have a sample of this to share in PHP though. Exactly what mechanism you use to store the access token will depend on what platform and libraries you're using. Keep in mind that the access token does enable access to the account, so you should store it securely.

View solution in original post

Re: Cron job to upload files from server to Dropbox

jordonshaw
Helpful | Level 5

I wondered if that might be the case.  Thank you so much for your help!

Re: Cron job to upload files from server to Dropbox

caseyprovost
New member | Level 2

What if you are not acting on behalf of the users? For example, in my case, we are just pushing files to Dropbox as part of an app-sync/integration. Do we need to provision an API user and build a UI around re-authenticating them?

Re: Cron job to upload files from server to Dropbox

Greg-DB
Dropboxer

@caseyprovost In order to upload to Dropbox via the Dropbox API, you need to upload to some particular user account. If you don't need to connect to arbitrary end-user accounts, and are just connecting to your own account, for instance, you don't need to build the app authorization flow and UI. You can just get your own access token once and use that.

Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropboxer
  • User avatar
    caseyprovost New member | Level 2
  • User avatar
    jordonshaw Helpful | Level 5
What do Dropbox user levels mean?
Need more support?