Class OAuth2


public class OAuth2 extends Object

Represents a single OAuth2 authorization. To use this class you'll typically need to create it then add it to an OAuth2AuthenticationURLConnectionProcessor, which is then added to either ReportFactory.getURLConnectionProcessors() or a custom list of URLConnectionProcessor objects set on a custom URLConnectionFactory.

Most importantly it will need a CallbackHandler which is able to handle OAuth2Callback objects. The OAuth2StandaloneCallbackHandler is an example which does this - when its OAuth2StandaloneCallbackHandler.handle([]) method is called, it creates a local webserver on the specified port and writes a prompt to stdout with the URL to visit to authorize the request.

The configuration for OAuth2 is done with a Json structure. The following fields can be included

  • client_id - a string representing the client_id, which is normally supplied by the service
  • client_secret - a string representing the client_secret, which is normally supplied by the service
  • scope - the scope token, a single string with one or more words seperated by spaces
  • redirect_uri - the redirection endpoint URL, a URL which will be preregistered with the OAuth2 service - for example
  • auth_uri - the URL to call to retrieve an authorization token
  • token_uri - the URL to call with the authorization token to retrieve an access token
  • authorization_method - if the string "inline", the client_id and client_secret will be sent in the JSON object, otherwise they will be sent as an HTTP Authorization: Bearer header
  • protocol.auth - an option map of extra strings to be included in requests to the "auth_uri"
  • protocol.grant - an option map of extra strings to be included in requests to the "token_uri"
  • protocol.refresh - an option map of extra strings to be included in requests to the "token_uri" when refreshing a token
  • Constructor Details

  • Method Details

    • load

      public void load(Json json)
      Load the OAuth2 configuration. A reference to the supplied Json object is kept, and the structure will be updated and should be saved if isUpdated() is true
      json - a Json which must have "client_id", "client_secret", "scope", "redirect_uri", "auth_uri" and "token_uri" strings. It may have an "authorization_method" string set to "inline"; It may have an "authorization" map with one or more of "access_token", "token_type", "refresh_token", "expires"; and it may have "protocol.auth", "protocol.grant" or "protocol.refresh" maps
    • isUpdated

      public boolean isUpdated()
      If the OAuth2 configuration supplied in load(Json) has been updated with new tokens, since the last time this method was called, return true.
    • getAccessToken

      public String getAccessToken(CallbackHandler handler) throws IOException
      Return the "access_token" from the getAuthorization( method.
    • getAuthorization

      public Json getAuthorization(CallbackHandler handler) throws IOException
      Return the authorization response from the OAuth2 server. The returned Json should have "access_token" and other properties returned from the server, except that "expires_in" is replaced with "expires" which is milliseconds since the epoch, and "id_token", if present, has been decoded. This method may block if the response needs to be requested or refreshed