Class DbxCredential

java.lang.Object
com.dropbox.core.oauth.DbxCredential

public class DbxCredential extends Object
Use this class to store the OAuth2 result. It wraps the access token, refresh token, token expiration time as well app key and app secret, which is used for refreshing call. The object can be serialized to or deserialized from persistent storage. DbxClientV2 and DbxTeamClientV2 use this class to construct clients supporting short-live token and refresh token.
  • Field Details

    • EXPIRE_MARGIN

      public static final long EXPIRE_MARGIN
      The margin that we think access token is "about to" expire. Within this margin, dropbox client would think the token is already expired and automatically refresh if possible.
      See Also:
    • Reader

      public static final JsonReader<DbxCredential> Reader
    • Writer

      public static final JsonWriter<DbxCredential> Writer
  • Constructor Details

    • DbxCredential

      public DbxCredential(String accessToken)
      Create a DbxCredential object that doesn't support refreshing.
      Parameters:
      accessToken - Short live token or legacty long live token.
    • DbxCredential

      public DbxCredential(String accessToken, Long expiresAt, String refreshToken, String appKey)
      Create a DbxCredential object if your app uses PKCE. PKCE flow doesn't requrie app secret.
      Parameters:
      accessToken - Short-lived access token from OAuth flow.
      expiresAt - Expiration time in millisecond from OAuth flow.
      refreshToken - Refresh token from OAuth flow.
      appKey - You app's client id.
      See Also:
    • DbxCredential

      public DbxCredential(String accessToken, Long expiresAt, String refreshToken, String appKey, String appSecret)
      Create a DbxCredential object supporting refreshing short live tokens.
      Parameters:
      accessToken - Short-lived access token from OAuth flow.
      expiresAt - Expiration time in millisecond from OAuth flow.
      refreshToken - Refresh token from OAuth flow
      appKey - You app's client id.
      appSecret - You app's client secret.
  • Method Details

    • getAccessToken

      public String getAccessToken()
      Returns the OAuth access token to use for authorization with Dropbox servers.
      Returns:
      OAuth access token
    • getExpiresAt

      public Long getExpiresAt()
      Return the millisecond when accessToken is going to expire.
      Returns:
      ExpiresAt in millisecond.
    • getAppKey

      public String getAppKey()
    • getAppSecret

      public String getAppSecret()
    • getRefreshToken

      public String getRefreshToken()
      Return the refresh token which can be used to obtain new access token.
      Returns:
      Refresh Token.
    • aboutToExpire

      public boolean aboutToExpire()
      Returns:
      true if access token is already expired or current time is within the \300000L to expiration time.
    • refresh

      public DbxRefreshResult refresh(DbxRequestConfig requestConfig, DbxHost host, Collection<String> scope) throws DbxException
      This should be used only in internal testing. Same as refresh(DbxRequestConfig) but just add host.
      Parameters:
      requestConfig - request config used to make http request.
      host - which host to call, only used in internal testing.
      scope - space-delimited scope list. Must be subset of scope in current oauth2 grant.
      Returns:
      DbxRefreshResult which contains app key and app secret.
      Throws:
      DbxOAuthException - If refresh failed becasue of invalid refresh parameter or refresh token is revoked.
      DbxException - If refresh failed for general errors.
    • refresh

      public DbxRefreshResult refresh(DbxRequestConfig requestConfig) throws DbxException
      Refresh the short live access token. If succeeds, the access token and expiration time value inside this object will be overwritten to new values.
      Parameters:
      requestConfig - request config used to make http request.
      Returns:
      DbxRefreshResult which contains app key and app secret.
      Throws:
      DbxOAuthException - If refresh failed because of invalid refresh parameter or refresh token is invalid or revoked.
      DbxException - If refresh failed for general errors.
    • refresh

      public DbxRefreshResult refresh(DbxRequestConfig requestConfig, Collection<String> scope) throws DbxException
      Refresh the short live access token. If succeeds, the access token and expiration time value inside this object will be overwritten to new values.
      Parameters:
      requestConfig - request config used to make http request.
      scope - Must be a subset of original scopes requested with this OAuth2 grant. You can use this method to obtain a new short lived token with less access than the original one.
      Returns:
      DbxRefreshResult which contains app key and app secret.
      Throws:
      DbxOAuthException - If refresh failed because of invalid refresh parameter or refresh token is invalid or revoked.
      DbxException - If refresh failed for general errors.
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      The json string containing all fields.