dropbox-sdk 1.6.4 → 1.6.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +6 -0
- data/README +9 -0
- data/examples/chunked_upload.rb +47 -47
- data/examples/cli_example.rb +172 -172
- data/examples/copy_between_accounts.rb +106 -106
- data/examples/dropbox_controller.rb +70 -70
- data/examples/oauth1_upgrade.rb +28 -28
- data/examples/search_cache.rb +231 -231
- data/examples/web_file_browser.rb +111 -111
- data/lib/dropbox_sdk.rb +1273 -1228
- metadata +66 -51
@@ -9,139 +9,139 @@ APP_SECRET = ''
|
|
9
9
|
STATE_FILE = 'copy_between_accounts.json'
|
10
10
|
|
11
11
|
def main()
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
12
|
+
prog_name = __FILE__
|
13
|
+
if APP_KEY == '' or APP_SECRET == ''
|
14
|
+
warn "ERROR: Set your APP_KEY and APP_SECRET at the top of #{prog_name}"
|
15
|
+
exit
|
16
|
+
end
|
17
|
+
args = ARGV
|
18
|
+
if args.size == 0
|
19
|
+
warn("Usage:\n")
|
20
|
+
warn(" #{prog_name} link Link to a user's account. Also displays UID.")
|
21
|
+
warn(" #{prog_name} list List linked users including UID.")
|
22
|
+
warn(" #{prog_name} copy '<uid>:<path>' '<uid>:<path>' Copies a file from the first user's path, to the second user's path.")
|
23
|
+
warn("\n\n <uid> is the account UID shown when linked. <path> is a path to a file on that user's dropbox.")
|
24
|
+
exit
|
25
|
+
end
|
26
|
+
|
27
|
+
command = args[0]
|
28
|
+
if command == 'link'
|
29
|
+
command_link(args)
|
30
|
+
elsif command == 'list'
|
31
|
+
command_list(args)
|
32
|
+
elsif command == 'copy'
|
33
|
+
command_copy(args)
|
34
|
+
else
|
35
|
+
warn "ERROR: Unknown command: #{command}"
|
36
|
+
warn "Run with no arguments for help."
|
37
|
+
exit(1)
|
38
|
+
end
|
39
39
|
end
|
40
40
|
|
41
41
|
def command_link(args)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
42
|
+
if args.size != 1
|
43
|
+
warn "ERROR: \"link\" doesn't take any arguments"
|
44
|
+
exit
|
45
|
+
end
|
46
|
+
|
47
|
+
web_auth = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET)
|
48
|
+
authorize_url = web_auth.start()
|
49
|
+
puts "1. Go to: #{authorize_url}"
|
50
|
+
puts "2. Click \"Allow\" (you might have to log in first)."
|
51
|
+
puts "3. Copy the authorization code."
|
52
|
+
|
53
|
+
print "Enter the authorization code here: "
|
54
|
+
STDOUT.flush
|
55
|
+
auth_code = STDIN.gets.strip
|
56
|
+
|
57
|
+
access_token, user_id = web_auth.finish(auth_code)
|
58
|
+
|
59
|
+
c = DropboxClient.new(access_token)
|
60
|
+
account_info = c.account_info()
|
61
|
+
puts "Link successful. #{account_info['display_name']} is uid #{account_info['uid']} "
|
62
|
+
|
63
|
+
state = load_state()
|
64
|
+
state[account_info['uid']] = {
|
65
|
+
'access_token' => access_token,
|
66
|
+
'display_name' => account_info['display_name'],
|
67
|
+
}
|
68
|
+
|
69
|
+
if account_info['team']
|
70
|
+
state[account_info['uid']]['team_name'] = account_info['team']['name']
|
71
|
+
end
|
72
|
+
|
73
|
+
save_state(state)
|
74
74
|
end
|
75
75
|
|
76
76
|
def command_list(args)
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
end
|
77
|
+
if args.size != 1
|
78
|
+
warn "ERROR: \"list\" doesn't take any arguments"
|
79
|
+
exit
|
80
|
+
end
|
81
|
+
|
82
|
+
state = load_state()
|
83
|
+
for e in state.keys()
|
84
|
+
if state[e]['team_name']
|
85
|
+
puts "#{state[e]['display_name']} (#{state[e]['team_name']}) is uid #{e}"
|
86
|
+
else
|
87
|
+
puts "#{state[e]['display_name']} is uid #{e}"
|
89
88
|
end
|
89
|
+
end
|
90
90
|
end
|
91
91
|
|
92
92
|
def command_copy(args)
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
93
|
+
if args.size != 3
|
94
|
+
warn "ERROR: \"copy\" takes exactly two arguments"
|
95
|
+
exit
|
96
|
+
end
|
97
97
|
|
98
|
-
|
98
|
+
state = load_state()
|
99
99
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
100
|
+
if state.keys().length < 2
|
101
|
+
warn "ERROR: You can't use the copy command until at least two users have linked"
|
102
|
+
exit
|
103
|
+
end
|
104
104
|
|
105
|
-
|
106
|
-
|
105
|
+
from = args[1].gsub(/['"]/,'')
|
106
|
+
to = args[2].gsub(/['"]/,'')
|
107
107
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
108
|
+
if not to.index(':') or not from.index(':')
|
109
|
+
warn "ERROR: Ill-formated paths. Run #{prog_name} without arugments to see documentation."
|
110
|
+
exit
|
111
|
+
end
|
112
112
|
|
113
|
-
|
114
|
-
|
113
|
+
from_uid, from_path = from.split ":"
|
114
|
+
to_uid, to_path = to.split ":"
|
115
115
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
116
|
+
if not state.has_key?(to_uid) or not state.has_key?(from_uid)
|
117
|
+
warn "ERROR: Those UIDs have not linked. Run #{prog_name} list to see linked UIDs."
|
118
|
+
exit
|
119
|
+
end
|
120
120
|
|
121
|
-
|
122
|
-
|
121
|
+
from_client = DropboxClient.new(state[from_uid]['access_token'])
|
122
|
+
to_client = DropboxClient.new(state[to_uid]['access_token'])
|
123
123
|
|
124
|
-
|
125
|
-
|
124
|
+
#Create a copy ref under the identity of the from user
|
125
|
+
copy_ref = from_client.create_copy_ref(from_path)['copy_ref']
|
126
126
|
|
127
|
-
|
127
|
+
metadata = to_client.add_copy_ref(to_path, copy_ref)
|
128
128
|
|
129
|
-
|
130
|
-
|
129
|
+
puts "File successly copied from #{state[from_uid]['display_name']} to #{state[to_uid]['display_name']}!"
|
130
|
+
puts "The file now exists at #{metadata['path']}"
|
131
131
|
|
132
132
|
end
|
133
133
|
|
134
134
|
def save_state(state)
|
135
|
-
|
136
|
-
|
137
|
-
|
135
|
+
File.open(STATE_FILE,"w") do |f|
|
136
|
+
f.write(JSON.pretty_generate(state))
|
137
|
+
end
|
138
138
|
end
|
139
139
|
|
140
140
|
def load_state()
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
141
|
+
if not FileTest.exists?(STATE_FILE)
|
142
|
+
return {}
|
143
|
+
end
|
144
|
+
JSON.parse(File.read(STATE_FILE))
|
145
145
|
end
|
146
146
|
|
147
147
|
|
@@ -28,86 +28,86 @@ APP_SECRET = ""
|
|
28
28
|
|
29
29
|
class DropboxController < ApplicationController
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
account_info = client.account_info
|
38
|
-
|
39
|
-
# Show a file upload page
|
40
|
-
render :inline =>
|
41
|
-
"#{account_info['email']} <br/><%= form_tag({:action => :upload}, :multipart => true) do %><%= file_field_tag 'file' %><%= submit_tag 'Upload' %><% end %>"
|
31
|
+
def main
|
32
|
+
client = get_dropbox_client
|
33
|
+
unless client
|
34
|
+
redirect_to(:action => 'auth_start') and return
|
42
35
|
end
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
37
|
+
account_info = client.account_info
|
38
|
+
|
39
|
+
# Show a file upload page
|
40
|
+
render :inline =>
|
41
|
+
"#{account_info['email']} <br/><%= form_tag({:action => :upload}, :multipart => true) do %><%= file_field_tag 'file' %><%= submit_tag 'Upload' %><% end %>"
|
42
|
+
end
|
49
43
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
rescue DropboxAuthError => e
|
55
|
-
session.delete(:access_token) # An auth error means the access token is probably bad
|
56
|
-
logger.info "Dropbox auth error: #{e}"
|
57
|
-
render :text => "Dropbox auth error"
|
58
|
-
rescue DropboxError => e
|
59
|
-
logger.info "Dropbox API error: #{e}"
|
60
|
-
render :text => "Dropbox API error"
|
61
|
-
end
|
44
|
+
def upload
|
45
|
+
client = get_dropbox_client
|
46
|
+
unless client
|
47
|
+
redirect_to(:action => 'auth_start') and return
|
62
48
|
end
|
63
49
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
50
|
+
begin
|
51
|
+
# Upload the POST'd file to Dropbox, keeping the same name
|
52
|
+
resp = client.put_file(params[:file].original_filename, params[:file].read)
|
53
|
+
render :text => "Upload successful. File now at #{resp['path']}"
|
54
|
+
rescue DropboxAuthError => e
|
55
|
+
session.delete(:access_token) # An auth error means the access token is probably bad
|
56
|
+
logger.info "Dropbox auth error: #{e}"
|
57
|
+
render :text => "Dropbox auth error"
|
58
|
+
rescue DropboxError => e
|
59
|
+
logger.info "Dropbox API error: #{e}"
|
60
|
+
render :text => "Dropbox API error"
|
75
61
|
end
|
62
|
+
end
|
76
63
|
|
77
|
-
|
78
|
-
|
79
|
-
|
64
|
+
def get_dropbox_client
|
65
|
+
if session[:access_token]
|
66
|
+
begin
|
67
|
+
access_token = session[:access_token]
|
68
|
+
DropboxClient.new(access_token)
|
69
|
+
rescue
|
70
|
+
# Maybe something's wrong with the access token?
|
71
|
+
session.delete(:access_token)
|
72
|
+
raise
|
73
|
+
end
|
80
74
|
end
|
75
|
+
end
|
81
76
|
|
82
|
-
|
83
|
-
|
77
|
+
def get_web_auth()
|
78
|
+
redirect_uri = url_for(:action => 'auth_finish')
|
79
|
+
DropboxOAuth2Flow.new(APP_KEY, APP_SECRET, redirect_uri, session, :dropbox_auth_csrf_token)
|
80
|
+
end
|
84
81
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
82
|
+
def auth_start
|
83
|
+
authorize_url = get_web_auth().start()
|
84
|
+
|
85
|
+
# Send the user to the Dropbox website so they can authorize our app. After the user
|
86
|
+
# authorizes our app, Dropbox will redirect them here with a 'code' parameter.
|
87
|
+
redirect_to authorize_url
|
88
|
+
end
|
89
89
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
end
|
90
|
+
def auth_finish
|
91
|
+
begin
|
92
|
+
access_token, user_id, url_state = get_web_auth.finish(params)
|
93
|
+
session[:access_token] = access_token
|
94
|
+
redirect_to :action => 'main'
|
95
|
+
rescue DropboxOAuth2Flow::BadRequestError => e
|
96
|
+
render :text => "Error in OAuth 2 flow: Bad request: #{e}"
|
97
|
+
rescue DropboxOAuth2Flow::BadStateError => e
|
98
|
+
logger.info("Error in OAuth 2 flow: No CSRF token in session: #{e}")
|
99
|
+
redirect_to(:action => 'auth_start')
|
100
|
+
rescue DropboxOAuth2Flow::CsrfError => e
|
101
|
+
logger.info("Error in OAuth 2 flow: CSRF mismatch: #{e}")
|
102
|
+
render :text => "CSRF error"
|
103
|
+
rescue DropboxOAuth2Flow::NotApprovedError => e
|
104
|
+
render :text => "Not approved? Why not, bro?"
|
105
|
+
rescue DropboxOAuth2Flow::ProviderError => e
|
106
|
+
logger.info "Error in OAuth 2 flow: Error redirect from Dropbox: #{e}"
|
107
|
+
render :text => "Strange error."
|
108
|
+
rescue DropboxError => e
|
109
|
+
logger.info "Error getting OAuth 2 access token: #{e}"
|
110
|
+
render :text => "Error communicating with Dropbox servers."
|
112
111
|
end
|
112
|
+
end
|
113
113
|
end
|
data/examples/oauth1_upgrade.rb
CHANGED
@@ -7,34 +7,34 @@ APP_KEY = ''
|
|
7
7
|
APP_SECRET = ''
|
8
8
|
|
9
9
|
def main
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
10
|
+
if APP_KEY == '' or APP_SECRET == ''
|
11
|
+
warn "ERROR: Set your APP_KEY and APP_SECRET at the top of search_cache.rb"
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
|
15
|
+
prog_name = __FILE__
|
16
|
+
args = ARGV
|
17
|
+
if args.size != 2
|
18
|
+
warn "Usage: #{prog_name} <oauth1-access-token-key> <oauth1-access-token-secret>"
|
19
|
+
exit 1
|
20
|
+
end
|
21
|
+
|
22
|
+
access_token_key = args[0]
|
23
|
+
access_token_secret = args[1]
|
24
|
+
|
25
|
+
sess = DropboxSession.new(APP_KEY, APP_SECRET)
|
26
|
+
sess.set_access_token(access_token_key, access_token_secret)
|
27
|
+
c = DropboxClient.new(sess)
|
28
|
+
|
29
|
+
print "Creating OAuth 2 access token...\n"
|
30
|
+
oauth2_access_token = c.create_oauth2_access_token
|
31
|
+
|
32
|
+
print "Using OAuth 2 access token to get account info...\n"
|
33
|
+
c2 = DropboxClient.new(oauth2_access_token)
|
34
|
+
pp c2.account_info
|
35
|
+
|
36
|
+
print "Disabling OAuth 1 access token...\n"
|
37
|
+
c.disable_access_token
|
38
38
|
end
|
39
39
|
|
40
40
|
main()
|