The authentication service is mainly used to obtain the token which will be used to stamp our CFDI (xml) already issued, In order to use this service, it is necessary that you have a username and a password to receive the token.
To use any of our services it is a must to have an access token. This token can be obtained in two ways:
Get token through username and password
In this section, we will show how to carry out the authentication and token generation, through our libraries and services.
Authenticate using API services
Authentication example using auth service:
1 2 3 4 |
POST /security/authenticate HTTP/1.1.. Host: services.test.sw.com.mx user: myusername password: mypassword |
Response example:
1 2 3 4 5 6 7 8 |
{ "data": { "token": "T2lYQ0t4L0RHVkR4...", "expires_in": 1595875169, "tokeny_type": "Bearer" }, "status": "success" } |
Authenticate using our libraries
You can also authenticate using our libraries, which includes c#, java, delphi, php and so on.
This is an authentication example using our java library and the Eclipse IDE. You can get the sdk library here.
- Create eclipse project.
- Import library to our project. In order to do this, go to the top menu and look for “project” → “Properties” and in the new window we will go to “Java Build Path” → “Libraries” → “Add External JARs …” and select the library.
- Create a new class.
- Use the the authentication function available in the sdk library.
1234567891011121314151617181920212223242526272829package AuthenticationExample;//Import libariesimport Services.Authentication.SWAuthenticationService;import Services.SWService;import Utils.Responses.AuthResponse;public class AuthTest {public static final String SERVICE_URL = "http://services.test.sw.com.mx";public static void main(){try{SWAuthenticationService auth;String username = "myusername";String password = "mypassword"auth = new SWAuthenticationService(username, password, SERVICE_URL);IResponse response;response = auth.Token();//Respose status examples: "success", "fail", "error"System.out.println(response.Status);//HTTP response status codeSystem.out.println(response.HttpStatusCode);//The response body, in this case is the tokenSystem.out.println(response.token);}catch (Exception e){//If the response status is "fail", or "error", it will throw an exceptionSystem.out.println(e.getMessage());}}}
Our services always return a “status”, which can be “success” or “error”.
In case we get a “status” different to “success” we will print the variables of the response object: the “message” and “messageDetail” which will tell us the error detail (example “Unauthorized”).
If our status is a “success”, we will print the token in the console, to verify that it works correctly.
This is the code example.
Get expireless token
Sometimes, for reasons of efficiency and speed of your application or system, you may not want to authenticate yourself and generate a token for each invoice (cfdi) that you send to stamp. For this case we have a token without expiration. To generate it follow the next steps.
- Step 1. Go to the stamps manager (Administrador de timbres) provided by SW sapien.
- Step 2. Enter your credentials (username and password) to login or use the demo account for testing purposes:
Username: demo
Password: 123456789
- Step 3. Once logged in, go to the top right corner section and click the gear icon
- Step 4. Click the option “Credenciales de acceso” (access credentials)
- Step 5. At that point you will already have your “expireless token” generated and you can use the copy (“Copiar”) button or copy the text manually to use it when you consume the SW smarter services.