Skip Navigation

Initializing and authenticating with the 
BlackBerry Web Services
 SOAP APIs

Before an application can make calls to the 
BlackBerry Web Services
 SOAP APIs, the application must initialize the BWS and BWSUtil web services.
When the BWS and BWSUtil web services are initialized, they accept subsequent API calls from the application. If initialization or authentication of a request are not successful, the application throws an exception. The exception contains a simple text message property that your application can access for more information.

Define metadata

Each method call contains a metadata object that specifies locale, client version, and organization ID data. The inclusion of this metadata helps supports compatibility with different versions of 
BlackBerry Web Services
. To learn more about metadata such as the 
ClientVersion
Locale
, and 
OrgUid
, see the API Reference.
Here's an example of how to create the 
RequestMetadata
 object and declare the variables used for authentication: 
// The request Metadata information. // This is the version of the WSDL used to generate the proxy, not the version of the server. private const string ClientVersion = "12.0.1"; // To use a different locale, call getLocales() in the BWSUtilService web service // to see which locales are supported. private const string Locale = "en_US"; private const string OrgUid = "0"; private static readonly RequestMetadata Metadata = new RequestMetadata(); // Authentication type name. private const string AuthenticatorName = "BlackBerry Administration Service"; // Hostname to use when connecting to web service. Includes port. private static string BWSHostName = null; // e.g. BWSHostName = "server01.yourcompany.net:18084" private static string Username = null; // e.g. Username = "admin" private static string Password = null; // e.g. Password = "password"
To send requests, you must provide authentication info, including the host name, user name, and password values.
// Hostname to use when connecting to web service. BWSHostName = "<BWSHostName>"; // e.g. BWSHostName = "server01.yourcompany.net". Username = "<username>"; // e.g. Username = "admin". Password = "<password>"; // e.g. Password = "password".

Assign values to the Metadata object

Here's how to set the client version, locale, and organization ID of the metadata object that was previously created:
Metadata.clientVersion = ClientVersion; Metadata.locale = Locale; Metadata.organizationUid = OrgUid;

Initialize and set the URL properties of the web services

Next, you initialize and set the values for the URL properties of the web services so that the application can connect to the 
BlackBerry Web Services
.
logMessage("Initializing BWS web service stub"); bwsService = new BWSService(); logMessage("BWS web service stub initialized"); logMessage("Initializing BWSUtil web service stub"); bwsUtilService = new BWSUtilService(); logMessage("BWSUtil web service stub initialized"); // These are the URLs that point to the web services used for all calls. bwsService.Url = "https://" + BWSHostName + "/enterprise/admin/ws"; bwsUtilService.Url = "https://" + BWSHostName + "/enterprise/admin/util/ws"; // Set the connection timeout to 60 seconds. bwsService.Timeout = 60000; bwsUtilService.Timeout = 60000;

Define the authenticator object

The following code defines the 
Authenticator
 object that the application requires for the initialization and authentication process. In the sections that follow, the application uses the authenticator object to collect the login information and the encoded user name that the application uses to authenticate with the 
BlackBerry Web Services
.
Authenticator authenticator = GetAuthenticator(AuthenticatorName); if (authenticator != null) { string encodedUsername = GetEncodedUserName(Username, authenticator); if (!string.IsNullOrEmpty(encodedUsername)) { /* * Set the HTTP basic authentication on the BWS service. * BWSUtilService is a utility web service that does not require * authentication. */ bwsService.Credentials = new NetworkCredential(encodedUsername, Password); /* * Send an HTTP Authorization header with requests after authentication * has taken place. */ bwsService.PreAuthenticate = true; returnValue = true; } else { logMessage("'encodedUsername' is null or empty"); } } else { logMessage("'authenticator' is null"); }

Authenticate with the 
BlackBerry Web Services

After the 
Authenticator
 object is created, here's how to retrieve the encoded login information for the administrator account that the app uses, and authenticate the app with the 
BlackBerry Web Services
.
public static string GetEncodedUserName(string username, Authenticator authenticator) { const string methodName = "GetEncodedUserName()"; const string bwsApiName = "bwsUtilService.getEncodedUsername()"; logMessage("Entering {0}", methodName); string returnValue = null; GetEncodedUsernameRequest request = new GetEncodedUsernameRequest(); request.metadata = Metadata; request.username = username; request.orgUid = Metadata.organizationUid; request.authenticator = authenticator; CredentialType credentialType = new CredentialType(); credentialType.PASSWORD = true; credentialType.value = "PASSWORD"; request.credentialType = credentialType; GetEncodedUsernameResponse response = null; try { logRequest(bwsApiName); response = bwsUtilService.getEncodedUsername(request); logResponse(bwsApiName, response.returnStatus.code, response.metadata); } catch (WebException e) { // Log and re-throw exception. logMessage("Exiting {0} with exception \"{1}\"", methodName, e.Message); throw e; } if (response.returnStatus.code.Equals("SUCCESS")) { returnValue = response.encodedUsername; } else { logMessage("Error Message: \"{0}\"", response.returnStatus.message); } try { logMessage("Decoded value of encoded username \"{0}\"", Encoding.Default.GetString(Convert.FromBase64String(returnValue))); } catch (Exception) { // Not actually base64 encoded. Show actual value logMessage("Value of encoded username \"{0}\"", returnValue); } logMessage("Exiting {0}", methodName); return returnValue; }
When the app completes the initialization and authentication process, the 
BlackBerry Web Services
 are ready to accept API calls from the application.