|
25 | 25 | * String userLocale = ... |
26 | 26 | * {@link DbxRequestConfig} requestConfig = new DbxRequestConfig("text-edit/0.1", userLocale); |
27 | 27 | * {@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); |
30 | 32 | * String sessionKey = "dropbox-auth-csrf-token"; |
31 | 33 | * 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); |
33 | 37 | * </pre> |
34 | 38 | * |
35 | 39 | * <p> |
36 | | - * Example, part 1 (doesn't include error handling code): |
| 40 | + * Part 1 (handler for "http://my-server.com/dropbox-auth-start"). |
37 | 41 | * </p> |
38 | 42 | * <pre> |
| 43 | + * javax.servlet.http.HttpServletResponse response = ... |
| 44 | + * |
39 | 45 | * <b>// Start authorization.</b> |
40 | | - * String authorizeUrl = auth.{@link #start start}(); |
| 46 | + * String authorizePageUrl = auth.{@link #start start}(); |
41 | 47 | * |
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); |
45 | 52 | * </pre> |
46 | 53 | * |
47 | 54 | * <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"). |
50 | 56 | * </p> |
51 | 57 | * <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 = ... |
61 | 59 | * |
62 | | - * <b>// Finish authorization to get an "access token".</b> |
63 | 60 | * {@link DbxAuthFinish} authFinish; |
64 | 61 | * 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; |
66 | 93 | * } |
67 | | - * DbxAccessToken accessToken = authResponse.accessToken; |
| 94 | + * String accessToken = authResponse.accessToken; |
68 | 95 | * |
69 | 96 | * <b>// Save the access token somewhere (probably in your database) so you</b> |
70 | 97 | * <b>// don't need to send the user through the authorization process again.</b> |
71 | 98 | * ... |
72 | 99 | * |
73 | 100 | * <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); |
75 | 102 | * ... |
76 | 103 | * </pre> |
77 | 104 | */ |
|
0 commit comments