Skip to content

Commit a2fc95b

Browse files
author
Kannan Goundan
committed
DbxWebAuth: Update example in Javadoc to OAuth 2.
Also made minor tweaks to web-file-browser example's /dropbox-auth-finish handler.
1 parent 4fb2d4a commit a2fc95b

2 files changed

Lines changed: 53 additions & 25 deletions

File tree

examples/web-file-browser/src/com/dropbox/core/examples/web_file_browser/DropboxAuth.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void doStart(HttpServletRequest request, HttpServletResponse response)
3737
String authorizeUrl = getWebAuth(request).start();
3838

3939
// Redirect the user to the Dropbox website so they can approve our application.
40-
// They'll be sent back to /dropbox-auth-finish when they're done.
40+
// The Dropbox website will send them back to /dropbox-auth-finish when they're done.
4141
response.sendRedirect(authorizeUrl);
4242
}
4343

@@ -86,11 +86,12 @@ public void doFinish(HttpServletRequest request, HttpServletResponse response)
8686
}
8787
catch (DbxWebAuth.ProviderException e) {
8888
common.log.println("On /dropbox-auth-finish: Auth failed: " + e.getMessage());
89-
response.sendError(500, "Error communicating with Dropbox.");
89+
response.sendError(503, "Error communicating with Dropbox.");
9090
return;
9191
}
9292
catch (DbxException e) {
9393
common.log.println("On /dropbox-auth-finish: Error getting token: " + e);
94+
response.sendError(503, "Error communicating with Dropbox.");
9495
return;
9596
}
9697

src/com/dropbox/core/DbxWebAuth.java

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,80 @@
2525
* String userLocale = ...
2626
* {@link DbxRequestConfig} requestConfig = new DbxRequestConfig("text-edit/0.1", userLocale);
2727
* {@link DbxAppInfo} appInfo = DbxAppInfo.Reader.readFromFile("api.app");
28-
* String redirectUri = "http://my-server.com/dropbox-auth-finish"
29-
* HttpSession session = request.getSession(true);
28+
*
29+
* <b>// Select a spot in the session for DbxWebAuth to store the CSRF token.</b>
30+
* javax.servlet.http.HttpServletRequest request = ...
31+
* javax.servlet.http.HttpSession session = request.getSession(true);
3032
* String sessionKey = "dropbox-auth-csrf-token";
3133
* DbxSessionStore csrfTokenStore = new DbxStandardSessionStore(session, sessionKey);
32-
* DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo, redirectUri, csrfTokenStore);
34+
*
35+
* String redirectUri = "http://my-server.com/dropbox-auth-finish"
36+
* DbxWebAuth auth = new DbxWebAuth(requestConfig, appInfo, redirectUri, csrfTokenStore);
3337
* </pre>
3438
*
3539
* <p>
36-
* Example, part 1 (doesn't include error handling code):
40+
* Part 1 (handler for "http://my-server.com/dropbox-auth-start").
3741
* </p>
3842
* <pre>
43+
* javax.servlet.http.HttpServletResponse response = ...
44+
*
3945
* <b>// Start authorization.</b>
40-
* String authorizeUrl = auth.{@link #start start}();
46+
* String authorizePageUrl = auth.{@link #start start}();
4147
*
42-
* <b>// Send user to Dropbox authorization page, which will redirect back to your</b>
43-
* <b>// "callback URL" after the user authorizes your app (part 2, below).</b>
44-
* sendRedirect(authUrl);
48+
* <b>// Redirect the user to the Dropbox website so they can approve our application.</b>
49+
* <b>// The Dropbox website will send them back to "http://my-server.com/dropbox-auth-finish"</b>
50+
* <b>// when they're done.</b>
51+
* response.sendRedirect(authorizePageUrl);
4552
* </pre>
4653
*
4754
* <p>
48-
* Servlet Example, part 2 (handler for "http://my-server.com/dropbox-auth"; doesn't
49-
* include error-handling code)
55+
* Part 2 (handler for "http://my-server.com/dropbox-auth-finish").
5056
* </p>
5157
* <pre>
52-
* <b>// Load the request token we saved in part 1.</b>
53-
* DbxRequestToken requestToken = (RequestToken) session.getAttribute("dropbox-request-token");
54-
* if (requestToken == null) return error("Couldn't find request token in session.");
55-
* session.removeAttribute("dropbox-request-token");
56-
*
57-
* <b>// Check 'oauth_token' to make sure this request is really from Dropbox.</b>
58-
* String key = request.getParameter("oauth_token");
59-
* if (key == null) return error("Missing parameter 'oauth_token'.");
60-
* if (!secureStringEquals(key, requestToken.key)) return error("Invalid 'oauth_token' parameter.");
58+
* javax.servlet.http.HttpServletResponse response = ...
6159
*
62-
* <b>// Finish authorization to get an "access token".</b>
6360
* {@link DbxAuthFinish} authFinish;
6461
* try {
65-
* authFinish = auth.{@link #finish finish}(requestToken);
62+
* authFinish = auth.{@link #finish finish}(request.getParameterMap());
63+
* }
64+
* catch (DbxWebAuth.BadRequestException ex) {
65+
* log("On /dropbox-auth-finish: Bad request: " + ex.getMessage());
66+
* response.sendError(400);
67+
* return;
68+
* }
69+
* catch (DbxWebAuth.BadStateException ex) {
70+
* // Send them back to the start of the auth flow.
71+
* response.sendRedirect("http://my-server.com/dropbox-auth-start");
72+
* return;
73+
* }
74+
* catch (DbxWebAuth.CsrfException ex) {
75+
* log("On /dropbox-auth-finish: CSRF mismatch: " + ex.getMessage());
76+
* return;
77+
* }
78+
* catch (DbxWebAuth.NotApprovedException ex) {
79+
* <b>// When Dropbox asked "Do you want to allow this app to access your</b>
80+
* <b>// Dropbox account?", the user clicked "No".</b>
81+
* ...
82+
* return;
83+
* }
84+
* catch (DbxWebAuth.ProviderException ex) {
85+
* log("On /dropbox-auth-finish: Auth failed: " + ex.getMessage());
86+
* response.sendError(503, "Error communicating with Dropbox.");
87+
* return;
88+
* }
89+
* catch (DbxException ex) {
90+
* log("On /dropbox-auth-finish: Error getting token: " + ex.getMessage());
91+
* response.sendError(503, "Error communicating with Dropbox.");
92+
* return;
6693
* }
67-
* DbxAccessToken accessToken = authResponse.accessToken;
94+
* String accessToken = authResponse.accessToken;
6895
*
6996
* <b>// Save the access token somewhere (probably in your database) so you</b>
7097
* <b>// don't need to send the user through the authorization process again.</b>
7198
* ...
7299
*
73100
* <b>// Now use the access token to make Dropbox API calls.</b>
74-
* {@link DbxClient} client = new DbxClient(requestConfig, authFinish.accessToken);
101+
* {@link DbxClient} client = new DbxClient(requestConfig, accessToken);
75102
* ...
76103
* </pre>
77104
*/

0 commit comments

Comments
 (0)