Authenticating with custom headers in the Connect SDK for Android
With the Connect SDK for the Android platform, you can authenticate to a Pega® Platform application by using custom headers. Custom headers are a feature of the OAuth 2.0 authorization protocol that allow client applications such as mobile apps to use third-party tokens to authenticate with a server. Several Java methods are available in the Connect SDK for Android for this purpose.
Adding a custom header
Use the addHeader()
method in the configuration
class to add a custom header consisting of a header name and a header value:
pega.configuration().addHeader("<header_name>", "<header_value>");
You can also use the addAuthorizationHeader()
method for the configuration
class to add an authorization header. An authorization header is a type of a custom header that you can use to authenticate with a server. For example, to add an authorization header with a value of MY_ACCESS_TOKEN, you can use the following code:
pega.configuration().addAuthorizationHeader("Bearer MY_ACCESS_TOKEN");
Authenticating without custom headers
When you authenticate to a Pega Platform application by using custom headers with one of the methods listed above, you do not need to use the authenticator()
method. If you do not use custom headers with your application, you can authenticate asynchronously by using the login()
method:
try{ pega.authenticator().login("username", "password"); } catch(ApiException e) { // login failed } //login succeeded
You can also authenticate synchronously with the login()
method:
pega.authenticator().login("username", "password", new Authentication.LoginCallback() { @Override void onSuccess() { // login succeeded } @Override void onFailure(ApiException e) { // login failed } });
Removing custom headers
If you use custom headers with the Connect SDK for Android, you can remove them when they are no longer needed in your application.
- To remove an authorization header, call the following method:
pega.configuration().removeAuthorizationHeader();
- To remove a single custom header that contains a header name, call the following method:
pega.configuration().removeHeader("<header_name>");
- To remove all custom headers, call the following method:
pega.configuration().removeAllHeaders();