-
Notifications
You must be signed in to change notification settings - Fork 444
/
ScopeAuthorize.java
167 lines (137 loc) · 6.89 KB
/
ScopeAuthorize.java
1
2
3
4
5
6
7
8
9
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
38
39
40
41
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.dropbox.core.examples.authorize;
import com.dropbox.core.DbxAppInfo;
import com.dropbox.core.DbxAuthFinish;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxWebAuth;
import com.dropbox.core.IncludeGrantedScopes;
import com.dropbox.core.TokenAccessType;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
/**
* You must have an api app migrated to new permission to run this example. You app must have
* scopes "account_info.read", "files.metadata.write", "files.content.read",
* "files.content.write", and "sharing.read" to run this example.
* You can manage your app's scopes inside your app console's permisison tab.
*
* This example goes through 3 different flows:
* Step 1: Kick off initial authorization with only "account_info.read" scope
* Step 2: Request a different scope "files.metadata.write"
* Step 3: Request "files.content.read", "files.content.write", and "sharing.read",
* along with all previously granted scopes using include_granted_scopes.
*/
public class ScopeAuthorize {
public DbxAuthFinish authorize(DbxAppInfo appInfo) throws IOException {
// Run through Dropbox API authorization process
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
// OAuth2 flow 1: Ask for account_info.read scope. Get a token with account_info.read
DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder()
.withNoRedirect()
.withTokenAccessType(TokenAccessType.OFFLINE)
.withScope(Collections.singletonList("account_info.read"))
.build();
String authorizeUrl = webAuth.authorize(webAuthRequest);
System.out.println("1. Go to " + authorizeUrl);
System.out.println("2. Click \"Allow\" (you might have to log in first).");
System.out.println("3. Copy the authorization code.");
System.out.print("Enter the authorization code here: ");
String code = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (code == null) {
System.exit(1);
}
code = code.trim();
try {
DbxAuthFinish authFinish = webAuth.finishFromCode(code);
assert authFinish.getScope().contains("account_info.read");
if (!authFinish.getScope().contains("account_info.read")) {
System.err.println("You app doesn't have account_info.read scope. Can't finish " +
"this example.");
System.exit(1); return null;
}
System.out.println("Successfully requested scope " + authFinish.getScope());
} catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
System.exit(1); return null;
}
// OAuth2 flow 2: Ask for files.metadata.write only. Get a token with files.metadata
// .write and its dependency files.metadata.read.
webAuthRequest = DbxWebAuth.newRequestBuilder()
.withNoRedirect()
.withTokenAccessType(TokenAccessType.OFFLINE)
.withScope(Collections.singletonList("files.metadata.write"))
.build();
authorizeUrl = webAuth.authorize(webAuthRequest);
System.out.println("1. Go to " + authorizeUrl);
System.out.println("2. Click \"Allow\" (you might have to log in first).");
System.out.println("3. Copy the authorization code.");
System.out.print("Enter the authorization code here: ");
code = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (code == null) {
System.exit(1);
}
code = code.trim();
try {
DbxAuthFinish authFinish = webAuth.finishFromCode(code);
if (!authFinish.getScope().contains("files.metadata.write")) {
System.err.println("You app doesn't have files.metadata.write scope. Can't finish " +
"this example.");
System.exit(1); return null;
}
assert !authFinish.getScope().contains("account_info.read");
System.out.println("Successfully requested scope " + authFinish.getScope());
} catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
System.exit(1); return null;
}
// Oauth2 flow 3: Ask for "files.content.read", "files.content.write" and "sharing.read",
// along with all previously granted scopes.
webAuthRequest = DbxWebAuth.newRequestBuilder()
.withNoRedirect()
.withTokenAccessType(TokenAccessType.OFFLINE)
.withScope(Arrays.asList("files.content.read", "files.content.write", "sharing.read"))
.withIncludeGrantedScopes(IncludeGrantedScopes.USER)
.build();
authorizeUrl = webAuth.authorize(webAuthRequest);
System.out.println("1. Go to " + authorizeUrl);
System.out.println("2. Click \"Allow\" (you might have to log in first).");
System.out.println("3. Copy the authorization code.");
System.out.print("Enter the authorization code here: ");
code = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (code == null) {
System.exit(1);
}
code = code.trim();
try {
DbxAuthFinish authFinish = webAuth.finishFromCode(code);
if (!authFinish.getScope().contains("files.content.read")) {
System.err.println("You app doesn't have files.content.read scope. Can't finish " +
"this example.");
System.exit(1); return null;
}
if (!authFinish.getScope().contains("files.content.write")) {
System.err.println("You app doesn't have files.content.write scope. Can't finish" +
" " +
"this example.");
System.exit(1); return null;
}
if (!authFinish.getScope().contains("sharing.read")) {
System.err.println("You app doesn't have sharing.read scope. Can't finish" +
" " +
"this example.");
System.exit(1); return null;
}
assert authFinish.getScope().contains("account_info.read");
assert authFinish.getScope().contains("files.metadata.write");
assert authFinish.getScope().contains("sharing.read");
System.out.println("Successfully requested scope " + authFinish.getScope());
return authFinish;
} catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
System.exit(1); return null;
}
}
}