public class OAuth2Service extends java.lang.Object implements OAuthService
Implements OAuth2 authentication for jMautic. If you want to connect to a Mautic instance
using OAuth2, you need to create a instance of the OAuth2Service class, configure it with your
Mautic instance URL, API keys and secrets and other things, and pass this instance to the
MauticApi
constructor.
The first time you use this class to connect to your Mautic instance, you need to configure
it with an instanceUrl (the URL to your Mautic instalation, such as
"https://mautic.myserver.com"
), an API key and secret, and a callbackUrl (such as
"https://yourapp.com/callback"
).
Then, you need to redirect your user to the URL returned by getAuthorizationUrl()
method. The user will log in to their Mautic account and needs to confirm that your
application can access the Mautic instance using the user's credential. If the user confirms,
Mautic will redirect the user to the given callbackUrl
, including an
code
parameter in the URL. For example, if your callbackUrl
is
"https://yourapp.com/callback"
, mautic can redirect the user to
"https://yourapp.com/callback?code=NTE4ZjVmOWM4MhNlOThjYWZhOGE3YjQ3MTgzYjFiZjQyZDU1NTi1MDQzNzFiNTcxMzU3YmQyNWNhOWI0YxVkNw"
.
You need to pass the value of the code
parameter (in the example above, the
String "NTE4ZjVmOWM4MhNlOThjYWZhOGE3YjQ3MTgzYjFiZjQyZDU1NTi1MDQzNzFiNTcxMzU3YmQyNWNhOWI0YxVkNw"
)
to the method setAuthorizationCode(String)
.
For example, you need to do the following:
// initialize the service OAuth2Service service = new OAuth2Service() .instanceUrl("https://mautic.myserver.com") .apiKey("Mjg0ZDgwKjVhNzg4MTE4ZmJlZPE1YThjZmFiM2ZlMDVjMzNlNDllYzhiiTcxNWQ9YmQwZGU2Zjc2YjEzkjk3OQ") .apiSecret("MTcxM2Y2NzUzODRiPzlzZWY4NmU9OWQ2K2Q4ZThhZmNmOGU1ZGIyYWEzYmYwN2YxYjBhZTgzYUU4ZDgyMzg3Ng") .callbackUrl("https://yourapp.com/callback") .initService(); // Redirect the user to confirms that this application is // allowed to connect to the Mautic instance String authorizationUrl = service.getAuthorizationUrl(); // Get the authorization code from the redirected URL and // pass it to the service String authorizationCode = "NTE4ZjVmOWM4MhNlOThjYWZhOGE3YjQ3MTgzYjFiZjQyZDU1NTi1MDQzNzFiNTcxMzU3YmQyNWNhOWI0YxVkNw"; service.setAuthorizationCode(authorizationCode);
After you set the authorization code, your OAuth2Service instance is ready to be used in the
MauticApi
constructor.
After you use the authorization code in the setAuthorizationCode(String)
method, you
can call getAccessToken()
and getRefreshToken()
methods to, respectivelly,
retrive the access and refresh tokens. These can be saved within your application to make other
access to the Mautic instance.
When you need to re-connect to the Mautic instance, you will configure the OAuth2Service
with the same instanceUrl
, apiKey
and apiSecret
from
the first call (the callbackUrl
is not needed). You also need to configure the
accessToken
you saved earlier. For example:
// Retrieve the access token you saved earlier from your storage String accessToken = "Mjg0ZDgyMjVhNzg9MTE4XmJlZjE1YThjZmFiM2ZlMDVjMzNlNDilYzhiZTcxNWQ4YmOwZGU2Zjc4YjEzZjk3OQ"; // initialize the service OAuth2Service service = new OAuth2Service() .instanceUrl("https://mautic.myserver.com") .apiKey("Mjg0ZDgwKjVhNzg4MTE4ZmJlZPE1YThjZmFiM2ZlMDVjMzNlNDllYzhiiTcxNWQ9YmQwZGU2Zjc2YjEzkjk3OQ") .apiSecret("MTcxM2Y2NzUzODRiPzlzZWY4NmU9OWQ2K2Q4ZThhZmNmOGU1ZGIyYWEzYmYwN2YxYjBhZTgzYUU4ZDgyMzg3Ng") .accessToken(accessToken) .initService();
You can use now the service instance in the MauticApi
constructor.
By default, the accessToken has a lifetime of 60 minutes. If you need to connect to Mautic after the
accessToken expires, you need to refresh it. To to this, pass the refreshToken
you
get on the first connection to the refreshToken(String)
method. For example:
// Retrieve the refresh token you saved earlier from your storage String refreshToken = "MXcxM2Y2NzUzODRiMcAzZWY4NmU2OWQ2n2Q4ZThhZmNmOGU4ZGIyYWEzYmYwN2YxZjBhZTgzYTU4ZDgyNzg3Ng"; // initialize the service OAuth2Service service = new OAuth2Service() .instanceUrl("https://mautic.myserver.com") .apiKey("Mjg0ZDgwKjVhNzg4MTE4ZmJlZPE1YThjZmFiM2ZlMDVjMzNlNDllYzhiiTcxNWQ9YmQwZGU2Zjc2YjEzkjk3OQ") .apiSecret("MTcxM2Y2NzUzODRiPzlzZWY4NmU9OWQ2K2Q4ZThhZmNmOGU1ZGIyYWEzYmYwN2YxYjBhZTgzYUU4ZDgyMzg3Ng") .initService(); // Refresh the token service.refreshToken(refreshToken);
By default, the refresh token is valid for 14 days. After that, you need to force the user to reauthenticate, as described in the First connection phase.
Internally, this OAuthService implementation uses ScribeJava to implement jMautic OAuth2 support.
Constructor and Description |
---|
OAuth2Service()
Creates a new OAuth2Service instance.
|
Modifier and Type | Method and Description |
---|---|
OAuth2Service |
accessToken(java.lang.String accessToken)
The access token you got after the First connection, when
you need to re-connect to the Mautic instance.
|
OAuth2Service |
apiKey(java.lang.String apiKey)
The API key (also known as "Public Key") for your Mautic instance.
|
OAuth2Service |
apiSecret(java.lang.String apiSecret)
The API secret (also known as "Secret Key") for your Mautic instance.
|
MauticApi |
build()
Returns an
MauticApi instance that uses this
OAuth2Service instance for authentication. |
OAuth2Service |
callbackUrl(java.lang.String callbackUrl)
The URL to your application that will handle the Mautic redirection after the user
authenticates and allow or deny your application to access the Mautic Instance.
|
java.io.InputStream |
executeRequest(Request request)
Execute the given request, returning an InputSteam to read the request response.
|
java.lang.String |
getAccessToken()
Returns the current access token.
|
java.lang.String |
getAuthorizationUrl()
Returns an URL based on your instanceUrl, that lets the user to allow or deny the access
from your application to the Mautic instance.
|
java.lang.String |
getRefreshToken()
Returns the current refresh token.
|
OAuth2Service |
initService()
Initialize some internal variables for this service.
|
OAuth2Service |
instanceUrl(java.lang.String instanceUrl)
Configures the instanceUrl of your Mautic instance.
|
boolean |
refreshToken(java.lang.String refreshToken)
Refresh the access token, using the given refreshToken.
|
void |
setAuthorizationCode(java.lang.String authorizationCode)
When doing the First connection, consumes the
authorization code and completes the authentication.
|
public OAuth2Service instanceUrl(java.lang.String instanceUrl)
instanceUrl
- the URL to your Mautic instancepublic OAuth2Service apiKey(java.lang.String apiKey)
apiKey
- the API key or Public Keypublic OAuth2Service apiSecret(java.lang.String apiSecret)
apiSecret
- the API secret or Secret keypublic OAuth2Service callbackUrl(java.lang.String callbackUrl)
callbackUrl
- a String with the URL to your applicationpublic OAuth2Service accessToken(java.lang.String accessToken)
accessToken
- the access tokenpublic java.lang.String getAuthorizationUrl()
public OAuth2Service initService()
public boolean refreshToken(java.lang.String refreshToken) throws MauticException
Refresh the access token, using the given refreshToken. After you refresh
the token, the current accessToken and refreshToken are invalid, and new
ones are available in the getAccessToken()
and getRefreshToken()
methods.
Please note that an refresh token is valid for some time (the default is 14 days, check your Mautic instance for your specific validity). After that, the refreshToken is invalid.
refreshToken
- a current valid refresh token.MauticException
- if an unexpected error occurspublic void setAuthorizationCode(java.lang.String authorizationCode) throws MauticOauthException
authorizationCode
- the code
parameter in your callback
URL, after the user authorizes your application to connect
to the mautic instance.MauticOauthException
- If an error occurs during the consumption of the authorization
codepublic java.lang.String getAccessToken()
public java.lang.String getRefreshToken()
public java.io.InputStream executeRequest(Request request) throws MauticException
OAuthService
executeRequest
in interface OAuthService
request
- the request that must be executedMauticException
- if any error occurs during the request. Errors may occur at
any level (connection problems, invalid request configuration,
Mautic internal errors, etc.). Please check the
Throwable.getMessage()
method for details.