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,
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 |
---|---|
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.
|
long |
getExpires()
Gets the amount of time for which this connection's access token is valid.
|
int |
getMaxRequestAttempts()
Gets the maximum number of times an API request will be tried when an error occurs.
|
String |
getRefreshToken()
Gets a refresh token that can be used to refresh an access token.
|
boolean |
needsRefresh()
Determines if this connection's access token has expired and needs to be refreshed.
|
void |
refresh()
Refresh's this connection's access token using its refresh token.
|
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 |
setMaxRequestAttempts(int attempts)
Sets the maximum number of times an API request will be tried when an error occurs.
|
void |
setRefreshToken(String refreshToken)
Sets the refresh token to use when refreshing an access token.
|
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 void setExpires(long milliseconds)
milliseconds
- the number of milliseconds for which the access token is valid.public long getExpires()
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 getAccessToken()
getAccessToken()
.public void setAccessToken(String accessToken)
accessToken
- a valid access token to use when authenticating API requests.public String getRefreshToken()
public void setRefreshToken(String refreshToken)
refreshToken
- a valid refresh token.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 boolean canRefresh()
public boolean needsRefresh()
public void refresh()
IllegalStateException
- if this connection's access token cannot be refreshed.