Anthony DeSimone
Anthony DeSimone

Reputation: 2034

Dropbox SDK for Ruby and not requiring reauthorization

It seems like I must be missing something here. I'm working on a simple app and I'm starting with Dropbox's tutorial:

# Install this the SDK with "gem install dropbox-sdk"
require 'dropbox_sdk'

# Get your app key and secret from the Dropbox developer website
APP_KEY     = 'XXXXXXXXXXXXXXX'
APP_SECRET  = 'XXXXXXXXXXXXXXX'
CODE        = 'QEL2VDUKRj4AAAAAAAAAAcBT_U9GoEvKF2UCXp3h4UA'

flow = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET)
puts flow.start()
access_token, user_id = flow.finish(CODE)

client = DropboxClient.new(access_token)

file = open('working-draft.txt')
response = client.put_file('/magnum-opus.txt', file)
puts "uploaded:", response.inspect

The code only seems to work for one request (or at least one run of the script), and then I get an error:

E:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/dropbox-sdk-1.6.2/lib/dropbox_sdk.rb:69:in `parse_response': invalid_grant (DropboxError)

What do I have to do in order to get this to work without authorizing every single time?

Upvotes: 1

Views: 362

Answers (1)

user94559
user94559

Reputation: 60143

Just store the access token somewhere and reuse it as much as you want.

To try this manually, just print out the value of access_token and then modify your code to just be:

access_token = '<value from previous run>'
client = DropboxClient.new(access_token)

Upvotes: 2

Related Questions