Class DbxWebAuth
- java.lang.Object
- 
- com.dropbox.core.DbxWebAuth
 
- 
 public class DbxWebAuth extends java.lang.ObjectDoes the OAuth 2 "authorization code" flow. (This SDK does not support the "token" flow.)Eventually yields an access token, which can be used with DbxClientV2to make Dropbox API calls. You typically only need to do this for a user when they first use your application. Once you have an access token for that user, it remains valid for years.Redirect exampleOne-time setup typically done on server initialization: DbxRequestConfigrequestConfig = new DbxRequestConfig("text-edit/0.1");DbxAppInfoappInfo = DbxAppInfo.Reader.readFromFile("api.app"); DbxWebAuth auth = new DbxWebAuth(requestConfig, appInfo); String redirectUri = "http://my-server.com/dropbox-auth-finish";Part 1Handler for "http://my-server.com/dropbox-auth-start": HttpServletRequestrequest = ...HttpServletResponseresponse = ... // Select a spot in the session for DbxWebAuth to store the CSRF token.HttpSessionsession = request.getSession(true); String sessionKey = "dropbox-auth-csrf-token";DbxSessionStorecsrfTokenStore = new DbxStandardSessionStore(session, sessionKey); // Build an auth requestDbxWebAuth.RequestauthRequest = DbxWebAuth.newRequestBuilder() .withRedirectUri(redirectUri, csrfTokenStore) .build(); // Start authorization. String authorizePageUrl = auth.authorize(authRequest); // Redirect the user to the Dropbox website so they can approve our application. // The Dropbox website will send them back to "http://my-server.com/dropbox-auth-finish" // when they're done. response.sendRedirect(authorizePageUrl);Part 2Handler for "http://my-server.com/dropbox-auth-finish": HttpServletRequestrequest = ...HttpServletResponseresponse = ... // Fetch the session to verify our CSRF tokenHttpSessionsession = request.getSession(true); String sessionKey = "dropbox-auth-csrf-token";DbxSessionStorecsrfTokenStore = new DbxStandardSessionStore(session, sessionKey); String redirectUri = "http://my-server.com/dropbox-auth-finish";DbxAuthFinishauthFinish; try { authFinish = auth.finishFromRedirect(redirectUri, csrfTokenStore, request.getParameterMap()); } catch (DbxWebAuth.BadRequestException ex) { log("On /dropbox-auth-finish: Bad request: " + ex.getMessage()); response.sendError(400); return; } catch (DbxWebAuth.BadStateException ex) { // Send them back to the start of the auth flow. response.sendRedirect("http://my-server.com/dropbox-auth-start"); return; } catch (DbxWebAuth.CsrfException ex) { log("On /dropbox-auth-finish: CSRF mismatch: " + ex.getMessage()); response.sendError(403, "Forbidden."); return; } catch (DbxWebAuth.NotApprovedException ex) { // When Dropbox asked "Do you want to allow this app to access your // Dropbox account?", the user clicked "No". ... return; } catch (DbxWebAuth.ProviderException ex) { log("On /dropbox-auth-finish: Auth failed: " + ex.getMessage()); response.sendError(503, "Error communicating with Dropbox."); return; } catch (DbxException ex) { log("On /dropbox-auth-finish: Error getting token: " + ex.getMessage()); response.sendError(503, "Error communicating with Dropbox."); return; } String accessToken = authFinish.getAccessToken(); // Save the access token somewhere (probably in your database) so you // don't need to send the user through the authorization process again. ... // Now use the access token to make Dropbox API calls.DbxClientV2client = new DbxClientV2(requestConfig, accessToken); ...No Redirect ExampleDbxRequestConfigrequestConfig = new DbxRequestConfig("text-edit/0.1");DbxAppInfoappInfo = DbxAppInfo.Reader.readFromFile("api.app"); DbxWebAuth auth = new DbxWebAuth(requestConfig, appInfo);DbxWebAuth.RequestauthRequest = DbxWebAuth.newRequestBuilder() .withNoRedirect() .build(); String authorizeUrl = auth.authorize(authRequest); 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 = System.console().readLine(); if (code != null) { code = code.trim();DbxAuthFinishauthFinish = webAuth.finishFromCode(code);DbxClientV2client = new DbxClientV2(requestConfig, authFinish.getAccessToken()); }
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classDbxWebAuth.BadRequestExceptionThrown when the parameters passed to your redirect URI are not well-formed.static classDbxWebAuth.BadStateExceptionThrown if all the parameters to your redirect URI are well-formed, but there's no CSRF token in the session.static classDbxWebAuth.CsrfExceptionThrown if the given 'state' parameter doesn't contain the expected CSRF token.static classDbxWebAuth.ExceptionThe base class for authorization redirect errors.static classDbxWebAuth.NotApprovedExceptionThrown when Dropbox tells us that the user chose not to grant your app access to their Dropbox account (i.e.static classDbxWebAuth.ProviderExceptionThrown when Dropbox tells us that some other error occurred in the authorization process.static classDbxWebAuth.RequestOAuth web-based authorization flow request.
 - 
Field SummaryFields Modifier and Type Field Description static java.lang.StringROLE_PERSONALRole representing the personal account associated with a user.static java.lang.StringROLE_WORKRole representing the team account associated with a user.
 - 
Constructor SummaryConstructors Constructor Description DbxWebAuth(DbxRequestConfig requestConfig, DbxAppInfo appInfo)Creates a new instance that will perform the OAuth2 authorization flow using the given OAuth request configuration.DbxWebAuth(DbxRequestConfig requestConfig, DbxAppInfo appInfo, java.lang.String redirectUri, DbxSessionStore sessionStore)Deprecated.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.Stringauthorize(DbxWebAuth.Request request)Starts authorization and returns an "authorization URL" on the Dropbox website that let the user grant your app access to their Dropbox account.DbxAuthFinishfinish(java.util.Map<java.lang.String,java.lang.String[]> queryParams)Deprecated.usefinishFromRedirect(..)instead.DbxAuthFinishfinishFromCode(java.lang.String code)Call this after the user has visited the authorizaton URL and copy/pasted the authorization code that Dropbox gave them.DbxAuthFinishfinishFromCode(java.lang.String code, java.lang.String redirectUri)Call this after the user has visited the authorizaton URL with a redirectUrl and copy/pasted the authorization code that Dropbox gave them.DbxAuthFinishfinishFromRedirect(java.lang.String redirectUri, DbxSessionStore sessionStore, java.util.Map<java.lang.String,java.lang.String[]> params)Call this after the user has visited the authorizaton URL and Dropbox has redirected them back to you at the redirect URI.static DbxWebAuth.Request.BuildernewRequestBuilder()Returns a new request builder with default values (e.g.java.lang.Stringstart(java.lang.String urlState)Deprecated.
 
- 
- 
- 
Field Detail- 
ROLE_WORKpublic static final java.lang.String ROLE_WORK Role representing the team account associated with a user. Used byDbxWebAuth.Request.Builder.withRequireRole(java.lang.String).- See Also:
- Constant Field Values
 
 - 
ROLE_PERSONALpublic static final java.lang.String ROLE_PERSONAL Role representing the personal account associated with a user. Used byDbxWebAuth.Request.Builder.withRequireRole(java.lang.String).- See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
DbxWebAuth@Deprecated public DbxWebAuth(DbxRequestConfig requestConfig, DbxAppInfo appInfo, java.lang.String redirectUri, DbxSessionStore sessionStore) Deprecated.Creates a new instance that will perform the OAuth2 authorization flow using a redirect URI.- Parameters:
- requestConfig- HTTP request configuration, never- null.
- appInfo- Your application's Dropbox API information (the app key and secret), never- nulL.
- redirectUri- Where to redirect the user after authorization has completed, never- null.
- sessionStore- Session store to use for storing CSRF nonces across requests, never- null.
 
 - 
DbxWebAuthpublic DbxWebAuth(DbxRequestConfig requestConfig, DbxAppInfo appInfo) Creates a new instance that will perform the OAuth2 authorization flow using the given OAuth request configuration.- Parameters:
- requestConfig- HTTP request configuration, never- null.
- appInfo- Your application's Dropbox API information (the app key and secret), never- null.
 
 
- 
 - 
Method Detail- 
start@Deprecated public java.lang.String start(java.lang.String urlState) Deprecated.Starts authorization and returns a "authorization URL" on the Dropbox website that gives the lets the user grant your app access to their Dropbox account.If a redirect URI was specified, then users will be redirected to the redirect URI after completing the authorization flow. Call finishFromRedirect(..)with the query parameters received from the redirect.If no redirect URI was specified, then users who grant access will be shown an "authorization code". The user must copy/paste the authorization code back into your app, at which point you can call finishFromCode(String)to get an access token.- Parameters:
- urlState- additional state to add to the flow that will be returned upon redirect
- Returns:
- Authorization URL of website user can use to authorize your app.
- Throws:
- java.lang.IllegalArgumentException- if urlState exceeds maximum size of 476 bytes
- java.lang.IllegalStateException- if this instance was not created using the deprecated- DbxWebAuth(DbxRequestConfig,DbxAppInfo,String,DbxSessionStore)constructor
 
 - 
authorizepublic java.lang.String authorize(DbxWebAuth.Request request) Starts authorization and returns an "authorization URL" on the Dropbox website that let the user grant your app access to their Dropbox account.If a redirect URI was specified ( DbxWebAuth.Request.Builder.withRedirectUri(java.lang.String, com.dropbox.core.DbxSessionStore)), then users will be redirected to the redirect URI after completing the authorization flow. CallfinishFromRedirect(java.lang.String, com.dropbox.core.DbxSessionStore, java.util.Map<java.lang.String, java.lang.String[]>)with the query parameters received from the redirect.If no redirect URI was specified ( DbxWebAuth.Request.Builder.withNoRedirect()), then users who grant access will be shown an "authorization code". The user must copy/paste the authorization code back into your app, at which point you can callfinishFromCode(String)to get an access token.- Parameters:
- request- OAuth 2.0 web-based authorization flow request configuration
- Returns:
- Authorization URL of website user can use to authorize your app.
- Throws:
- java.lang.IllegalStateException- if this- DbxWebAuthinstance was created using the deprecated- DbxWebAuth(DbxRequestConfig,DbxAppInfo,String,DbxSessionStore)constructor, or if this (@link DbxWebAuth} instance was created with- DbxAppInfowithout app secret.
 
 - 
finishFromCodepublic DbxAuthFinish finishFromCode(java.lang.String code) throws DbxException Call this after the user has visited the authorizaton URL and copy/pasted the authorization code that Dropbox gave them.- Parameters:
- code- The authorization code shown to the user when they clicked "Allow" on the authorization, page on the Dropbox website, never- null.
- Throws:
- DbxException- if an error occurs communicating with Dropbox.
 
 - 
finishFromCodepublic DbxAuthFinish finishFromCode(java.lang.String code, java.lang.String redirectUri) throws DbxException Call this after the user has visited the authorizaton URL with a redirectUrl and copy/pasted the authorization code that Dropbox gave them.- Parameters:
- code- The authorization code shown to the user when they clicked "Allow" on the authorization, page on the Dropbox website, never- null.
- redirectUri- The original redirect URI used by- authorize(com.dropbox.core.DbxWebAuth.Request), never- null.
- Throws:
- DbxException- if an error occurs communicating with Dropbox.
 
 - 
finishFromRedirectpublic DbxAuthFinish finishFromRedirect(java.lang.String redirectUri, DbxSessionStore sessionStore, java.util.Map<java.lang.String,java.lang.String[]> params) throws DbxException, DbxWebAuth.BadRequestException, DbxWebAuth.BadStateException, DbxWebAuth.CsrfException, DbxWebAuth.NotApprovedException, DbxWebAuth.ProviderException Call this after the user has visited the authorizaton URL and Dropbox has redirected them back to you at the redirect URI.- Parameters:
- redirectUri- The original redirect URI used by- authorize(com.dropbox.core.DbxWebAuth.Request), never- null.
- sessionStore- Session store used by- authorize(com.dropbox.core.DbxWebAuth.Request)to store CSRF tokens, never- null.
- params- The query parameters on the GET request to your redirect URI, never- null.
- Throws:
- DbxWebAuth.BadRequestException- If the redirect request is missing required query parameters, contains duplicate parameters, or includes mutually exclusive parameters (e.g.- "error"and- "code").
- DbxWebAuth.BadStateException- If the CSRF token retrieved from- sessionStoreis- nullor malformed.
- DbxWebAuth.CsrfException- If the CSRF token passed in- paramsdoes not match the CSRF token from- sessionStore. This implies the redirect request may be forged.
- DbxWebAuth.NotApprovedException- If the user chose to deny the authorization request.
- DbxWebAuth.ProviderException- If an OAuth2 error response besides- "access_denied"is set.
- DbxException- If an error occurs communicating with Dropbox.
 
 - 
finish@Deprecated public DbxAuthFinish finish(java.util.Map<java.lang.String,java.lang.String[]> queryParams) throws DbxException, DbxWebAuth.BadRequestException, DbxWebAuth.BadStateException, DbxWebAuth.CsrfException, DbxWebAuth.NotApprovedException, DbxWebAuth.ProviderException Deprecated.usefinishFromRedirect(..)instead.Call this after the user has visited the authorizaton URL and Dropbox has redirected them back to you (using theredirectUriyou passed in tostart(java.lang.String).- Parameters:
- queryParams- The query parameters on the GET request to your- redirectUri.
- Throws:
- java.lang.IllegalStateException- if this instance was not created using the deprecated- DbxWebAuth(DbxRequestConfig,DbxAppInfo,String,DbxSessionStore)constructor
- DbxWebAuth.BadRequestException- If the redirect request is missing required query parameters, contains duplicate parameters, or includes mutually exclusive parameters (e.g.- "error"and- "code")
- DbxWebAuth.BadStateException- If the CSRF token retrieved from- sessionStoreis missing or malformed. Missing tokens often imply the user session has expired.
- DbxWebAuth.CsrfException- If the CSRF token passed in- paramsdoes not match the CSRF token from- sessionStore. This implies the redirect request may be forged.
- DbxWebAuth.NotApprovedException- If the user chose to deny the authorization request
- DbxWebAuth.ProviderException- If an OAuth 2.0 error response besides- "access_denied"is set.
- DbxException- If an error occurs communicating with Dropbox
 
 - 
newRequestBuilderpublic static DbxWebAuth.Request.Builder newRequestBuilder() Returns a new request builder with default values (e.g. no redirect).- Returns:
- new request builder with default values
 
 
- 
 
-