public class BoxAPIConnection extends Object
This class handles storing authentication information, automatic token refresh, and rate-limiting. It can also be used to configure the Box API endpoint URL in order to hit a different version of the API. Multiple instances of BoxAPIConnection may be created to support multi-user login.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_ATTEMPTS
The default maximum number of times an API request will be tried when an error occurs.
|
Constructor and Description |
---|
BoxAPIConnection(BoxConfig boxConfig)
Constructs a new BoxAPIConnection levaraging BoxConfig.
|
BoxAPIConnection(String accessToken)
Constructs a new BoxAPIConnection that authenticates with a developer or access token.
|
BoxAPIConnection(String clientID,
String clientSecret)
Constructs a new BoxAPIConnection.
|
BoxAPIConnection(String clientID,
String clientSecret,
String authCode)
Constructs a new BoxAPIConnection with an auth code that was obtained from the first half of OAuth.
|
BoxAPIConnection(String clientID,
String clientSecret,
String accessToken,
String refreshToken)
Constructs a new BoxAPIConnection with an access token that can be refreshed.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(BoxAPIConnectionListener listener)
Add a listener to listen to Box API connection events.
|
void |
asSelf()
Sets this API connection to make API calls on behalf of the user with whom the access token is associated.
|
void |
asUser(String userID)
Set this API connection to make API calls on behalf of another users, impersonating them.
|
void |
authenticate(String authCode)
Authenticates the API connection by obtaining access and refresh tokens using the auth code that was obtained
from the first half of OAuth.
|
boolean |
canRefresh()
Determines if this connection's access token can be refreshed.
|
void |
enableNotifications()
Re-enable email notifications from API actions if they have been suppressed.
|
String |
getAccessToken()
Gets an access token that can be used to authenticate an API request.
|
static URL |
getAuthorizationURL(String clientID,
URI redirectUri,
String state,
List<String> scopes)
Return the authorization URL which is used to perform the authorization_code based OAuth2 flow.
|
boolean |
getAutoRefresh()
Gets whether or not automatic refreshing of this connection's access token is enabled.
|
String |
getBaseUploadURL()
Gets the base upload URL that's used when performing file uploads to Box.
|
String |
getBaseURL()
Gets the base URL that's used when sending requests to the Box API.
|
String |
getClientID()
Gets the client ID.
|
String |
getClientSecret()
Gets the client secret.
|
int |
getConnectTimeout()
Gets the connect timeout for this connection in milliseconds.
|
long |
getExpires()
Gets the amount of time for which this connection's access token is valid.
|
long |
getLastRefresh()
Gets the last time that the access token was refreshed.
|
ScopedToken |
getLowerScopedToken(List<String> scopes,
String resource)
Get a lower-scoped token restricted to a resource for the list of scopes that are passed.
|
int |
getMaxRequestAttempts()
Gets the maximum number of times an API request will be tried when an error occurs.
|
Proxy |
getProxy()
Gets the proxy value to use for API calls to Box.
|
String |
getProxyPassword()
Gets the password to use for a proxy that requires basic auth.
|
String |
getProxyUsername()
Gets the username to use for a proxy that requires basic auth.
|
int |
getReadTimeout()
Gets the read timeout for this connection in milliseconds.
|
protected ReadWriteLock |
getRefreshLock()
Gets the refresh lock to be used when refreshing an access token.
|
String |
getRefreshToken()
Gets a refresh token that can be used to refresh an access token.
|
RequestInterceptor |
getRequestInterceptor()
Gets the RequestInterceptor associated with this API connection.
|
String |
getRevokeURL()
Returns the URL used for token revocation.
|
String |
getTokenURL()
Gets the token URL that's used to request access tokens.
|
String |
getUserAgent()
Gets the user agent that's used when sending requests to the Box API.
|
boolean |
needsRefresh()
Determines if this connection's access token has expired and needs to be refreshed.
|
protected void |
notifyError(BoxAPIException error)
Notifies an error event to all the listeners.
|
protected void |
notifyRefresh()
Notifies a refresh event to all the listeners.
|
void |
refresh()
Refresh's this connection's access token using its refresh token.
|
void |
removeCustomHeader(String header)
Removes a custom header, so it will no longer be sent on requests through this API connection.
|
void |
removeListener(BoxAPIConnectionListener listener)
Remove a listener listening to Box API connection events.
|
void |
restore(String state)
Restores a saved connection state into this BoxAPIConnection.
|
static BoxAPIConnection |
restore(String clientID,
String clientSecret,
String state)
Restores a BoxAPIConnection from a saved state.
|
void |
revokeToken()
Revokes the tokens associated with this API connection.
|
String |
save()
Saves the state of this connection to a string so that it can be persisted and restored at a later time.
|
void |
setAccessToken(String accessToken)
Sets the access token to use when authenticating API requests.
|
void |
setAutoRefresh(boolean autoRefresh)
Enables or disables automatic refreshing of this connection's access token.
|
void |
setBaseUploadURL(String baseUploadURL)
Sets the base upload URL to be used when performing file uploads to Box.
|
void |
setBaseURL(String baseURL)
Sets the base URL to be used when sending requests to the Box API.
|
void |
setConnectTimeout(int connectTimeout)
Sets the connect timeout for this connection.
|
void |
setCustomHeader(String header,
String value)
Sets a custom header to be sent on all requests through this API connection.
|
void |
setExpires(long milliseconds)
Sets the amount of time for which this connection's access token is valid before it must be refreshed.
|
void |
setLastRefresh(long lastRefresh)
Sets the last time that the access token was refreshed.
|
void |
setMaxRequestAttempts(int attempts)
Sets the maximum number of times an API request will be tried when an error occurs.
|
void |
setProxy(Proxy proxy)
Sets the proxy to use for API calls to Box.
|
void |
setProxyPassword(String proxyPassword)
Sets the password to use for a proxy that requires basic auth.
|
void |
setProxyUsername(String proxyUsername)
Sets the username to use for a proxy that requires basic auth.
|
void |
setReadTimeout(int readTimeout)
Sets the read timeout for this connection.
|
void |
setRefreshToken(String refreshToken)
Sets the refresh token to use when refreshing an access token.
|
void |
setRequestInterceptor(RequestInterceptor interceptor)
Sets a RequestInterceptor that can intercept requests and manipulate them before they're sent to the Box API.
|
void |
setRevokeURL(String url)
Set the URL used for token revocation.
|
void |
setTokenURL(String tokenURL)
Sets the token URL that's used to request access tokens.
|
void |
setUserAgent(String userAgent)
Sets the user agent to be used when sending requests to the Box API.
|
void |
suppressNotifications()
Suppresses email notifications from API actions.
|
public static final int DEFAULT_MAX_ATTEMPTS
public BoxAPIConnection(String accessToken)
accessToken
- a developer or access token to use for authenticating with the API.public BoxAPIConnection(String clientID, String clientSecret, String accessToken, String refreshToken)
clientID
- the client ID to use when refreshing the access token.clientSecret
- the client secret to use when refreshing the access token.accessToken
- an initial access token to use for authenticating with the API.refreshToken
- an initial refresh token to use when refreshing the access token.public BoxAPIConnection(String clientID, String clientSecret, String authCode)
clientID
- the client ID to use when exchanging the auth code for an access token.clientSecret
- the client secret to use when exchanging the auth code for an access token.authCode
- an auth code obtained from the first half of the OAuth process.public BoxAPIConnection(String clientID, String clientSecret)
clientID
- the client ID to use when exchanging the auth code for an access token.clientSecret
- the client secret to use when exchanging the auth code for an access token.public BoxAPIConnection(BoxConfig boxConfig)
boxConfig
- BoxConfig file, which should have clientId and clientSecretpublic static BoxAPIConnection restore(String clientID, String clientSecret, String state)
public static URL getAuthorizationURL(String clientID, URI redirectUri, String state, List<String> scopes)
clientID
- the client ID to use with the connection.redirectUri
- the URL to which Box redirects the browser when authentication completes.state
- the text string that you choose.
Box sends the same string to your redirect URL when authentication is complete.scopes
- this optional parameter identifies the Box scopes available
to the application once it's authenticated.public void authenticate(String authCode)
authCode
- the auth code obtained from the first half of the OAuth process.public String getClientID()
public String getClientSecret()
public void setExpires(long milliseconds)
milliseconds
- the number of milliseconds for which the access token is valid.public long getExpires()
public String getTokenURL()
public void setTokenURL(String tokenURL)
tokenURL
- the token URL.public void setRevokeURL(String url)
url
- The url to use.public String getRevokeURL()
public String getBaseURL()
public void setBaseURL(String baseURL)
baseURL
- a base URLpublic String getBaseUploadURL()
public void setBaseUploadURL(String baseUploadURL)
baseUploadURL
- a base upload URL.public String getUserAgent()
public void setUserAgent(String userAgent)
userAgent
- the user agent.public String getAccessToken()
getAccessToken()
.public void setAccessToken(String accessToken)
accessToken
- a valid access token to use when authenticating API requests.protected ReadWriteLock getRefreshLock()
public String getRefreshToken()
public void setRefreshToken(String refreshToken)
refreshToken
- a valid refresh token.public long getLastRefresh()
public void setLastRefresh(long lastRefresh)
This value is used when determining if an access token needs to be auto-refreshed. If the amount of time since the last refresh exceeds the access token's expiration time, then the access token will be refreshed.
lastRefresh
- the new last refresh time in milliseconds.public void setAutoRefresh(boolean autoRefresh)
autoRefresh
- true to enable auto token refresh; otherwise false.public boolean getAutoRefresh()
public int getMaxRequestAttempts()
public void setMaxRequestAttempts(int attempts)
attempts
- the maximum number of request attempts.public int getConnectTimeout()
public void setConnectTimeout(int connectTimeout)
connectTimeout
- The number of milliseconds to wait for the connection to be established.public int getReadTimeout()
public void setReadTimeout(int readTimeout)
readTimeout
- The number of milliseconds to wait for bytes to be read.public Proxy getProxy()
public void setProxy(Proxy proxy)
proxy
- the proxy to use for API calls to Box.public String getProxyUsername()
public void setProxyUsername(String proxyUsername)
proxyUsername
- the username to use for a proxy that requires basic auth.public String getProxyPassword()
public void setProxyPassword(String proxyPassword)
proxyPassword
- the password to use for a proxy that requires basic auth.public boolean canRefresh()
public boolean needsRefresh()
public void refresh()
IllegalStateException
- if this connection's access token cannot be refreshed.public void restore(String state)
protected void notifyRefresh()
protected void notifyError(BoxAPIException error)
error
- A BoxAPIException instance.public void addListener(BoxAPIConnectionListener listener)
listener
- a listener to listen to Box API connection.public void removeListener(BoxAPIConnectionListener listener)
listener
- the listener to remove.public RequestInterceptor getRequestInterceptor()
public void setRequestInterceptor(RequestInterceptor interceptor)
interceptor
- the RequestInterceptor.public ScopedToken getLowerScopedToken(List<String> scopes, String resource)
scopes
- the list of scopes to which the new token should be restricted forresource
- the resource for which the new token has to be obtainedpublic void revokeToken()
public String save()
Note that proxy settings aren't automatically saved or restored. This is mainly due to security concerns around persisting proxy authentication details to the state string. If your connection uses a proxy, you will have to manually configure it again after restoring the connection.
restore(java.lang.String, java.lang.String, java.lang.String)
public void setCustomHeader(String header, String value)
header
- the header name.value
- the header value.public void removeCustomHeader(String header)
header
- the header name.public void suppressNotifications()
public void enableNotifications()
suppressNotifications()
public void asUser(String userID)
userID
- the ID of the user to act as.public void asSelf()
asUser(java.lang.String)