To communicate with the API, we must carry out the following steps:
- Create an appropriate request
- Set up that request
- Submit that request to the appropriate service
- Store the response and handle data + error notifications as required.
This post, I’m looking at logging in, which is a global service, therefore we will be submitting our request to the global service.
To login, I created a button on my form. When the button is clicked, the following code is executed:
To login, I created a button on my form. When the button is clicked, the following code is executed:
global.BFGlobalService bfGlobalService = new global.BFGlobalService();
LoginReq loginRequest = new LoginReq();
loginRequest.username = USERNAME;
loginRequest.password = PASSWORD;
loginRequest.productId = 82;
LoginResp loginResponse = bfGlobalService.login(loginRequest);
Obviously you replace USERNAME and PASSWORD with your login details. It’s up to you how as to whether you hardcode these into your file, or you enter them via a textbox on your form whenever you wish to log in. Lets look at this code line by line:
The first line creates the service that we are going to submit our request to. When I added my WSDL references, I added the Global Service to a namespace called “global”, hence the label preceding the variable type. The reason I have done this, and not added a using statement at the top, is that there are a couple of methods and types that are named the same in both the Global and Exchange services. While I’m fairly sure that these types are interchangeable, it’s not going to hurt to ensure we’re being as safe as possible.
The second line creates a new login request object, called loginRequest.
Lines three, four and five set up the properties of the login request.
The final line submits the request to the Global Service, and stores the response in a login response object. From here, you extract the relevant data (including the API header containing the session key, to be covered in a later post).
That’s all there is to it – you’re now logged in! If you’d like confirmation, write the loginResponse.errorCode to a label on your form – it will return “OK” if all is good – see what happens if you enter the wrong details.
It’s getting late now – I’ll work on bringing this blog up to my current development state over the next few days.
No comments:
Post a Comment