cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Discuss Dropbox Developer & API

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

Re: Cron job to upload files from server to Dropbox

Cron job to upload files from server to Dropbox

jordonshaw
Helpful | Level 5
Go to solution

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

Greg-DB
Dropbox Staff
Go to solution

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

Greg-DB
Dropbox Staff
Go to solution

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.

jordonshaw
Helpful | Level 5
Go to solution

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

caseyprovost
New member | Level 2
Go to solution

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?

Greg-DB
Dropbox Staff
Go to solution

@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.

Need more support?