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(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 |
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.
|
String |
getAccessToken()
Gets an access token that can be used to authenticate an API request.
|
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.
|
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.
|
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.
|
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 |
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 |
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.
|
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 |
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 |
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 |
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.
|
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 static BoxAPIConnection restore(String clientID, String clientSecret, String state)
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 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 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 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)