XeroAPI/Xero-Java

Accessing the PayrollAuApi while connected to Demo Company gives an exception

arifainchtein opened this issue · 4 comments

SDK you're using (please complete the following information):
Versuin 4.14

Describe the bug

I have written code that connects to the Demo Company. I can create invoices without an issue. I now need to develop a timesheet application and i get an Unauthorized exception while conecting:

To Reproduce
Here is the code:

ApiClient defaultPayrollAuClient = new ApiClient("https://api.xero.com/payroll.xro/1.0",null,null,null,null);
// Get Singleton - instance of PayrollAUAPi client
PayrollAuApi payrollAuApi = PayrollAuApi.getInstance(defaultPayrollAuClient);
//GET all Timesheets
Timesheets timesheets = payrollAuApi.getTimesheets(access_token, xero_tenant_id, null, null, null, new Integer(1));

Expected behavior
I would expect this code to work because it is from an example

Additional context
Here is the error that i got:

com.xero.api.XeroUnauthorizedException: Unauthorized - check your scopes and confirm access to this resource
at com.xero.api.XeroApiExceptionHandler.execute(XeroApiExceptionHandler.java:173)
at com.xero.api.client.PayrollAuApi.getTimesheets(PayrollAuApi.java:2517)
at au.com.aquabubbler.lucille.xero.XeroTimesheetUploader.process(XeroTimesheetUploader.java:197)
at au.com.aquabubbler.lucille.tools.XeroTimesheetUploaderTester.main(XeroTimesheetUploaderTester.java:27)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: com.google.api.client.http.HttpResponseException: 401 Unauthorized
{"Type":null,"Title":"Unauthorized","Status":401,"Detail":"AuthorizationUnsuccessful","Instance":"6df08333-14d6-4f26-be58-9e73a025e1c9","Extensions":{}}
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1070)
at com.xero.api.client.PayrollAuApi.getTimesheetsForHttpResponse(PayrollAuApi.java:2614)
at com.xero.api.client.PayrollAuApi.getTimesheets(PayrollAuApi.java:2505)

I am using the Demo company and from what i have read online, payroll suppose to be available when using the Demo Company. I dont see anywhere to configure the scopes for this object in the xero website.

Do you have any suggestions?
THanks

Looking up the instance id shows that the access token you are using is missing the payroll.timesheets scope.

Please can you go through the process of connecting your app to Xero and when you are generating the authorisation link, please can you add the payroll.timesheets scope to the list of scopes. When you reauthorise, the new scope will be added

https://developer.xero.com/documentation/guides/oauth2/scopes

hi, thanks for that, its working now

Actually, i did manage to get it to work and did not get the original error of this issue. However I am now getting a 500 error.

Here is the code:

try { ApiClient defaultPayrollAuClient = new ApiClient("https://api.xero.com/payroll.xro/1.0",null,null,null,null); // Get Singleton - instance of PayrollAUAPi client PayrollAuApi payrollAuApi = PayrollAuApi.getInstance(defaultPayrollAuClient);
			//GET all Timesheets
			Timesheets timesheets = payrollAuApi.getTimesheets(access_token, xero_tenant_id, null, null, null, new Integer(1));
			System.out.println("GET  Timesheets - total:" + timesheets.getTimesheets().size() );
			UUID timesheetID = timesheets.getTimesheets().get(0).getTimesheetID();

			//GET one Timesheet            
			TimesheetObject timesheet = payrollAuApi.getTimesheet(access_token, xero_tenant_id,timesheetID);
			System.out.println("GET one Timesheets - employee id:" + timesheet.getTimesheet().getEmployeeID() );

			//CREATE Timesheet
			PayItems payItems = payrollAuApi.getPayItems(access_token, xero_tenant_id, null, null, null, null);
			UUID ordinaryEarningsRateID = payItems.getPayItems().getEarningsRates().get(0).getEarningsRateID();     

			com.xero.models.payrollau.Employees employees = payrollAuApi.getEmployees(access_token, xero_tenant_id, null, null, null, null);
			Employee employee = employees.getEmployees().get(0);
			System.out.println("Employee=" + employee.getFirstName() + " " + employee.getLastName());

			UUID employeeID = employee.getEmployeeID();

			Timesheet newTimesheet = new Timesheet();
			List<Timesheet> newTimesheets = new ArrayList<>();

			//  newTimesheet.setStartDate(LocalDate.of(2019, 11, 8));
			//     newTimesheet.setEndDate(LocalDate.of(2019, 11, 14));
			newTimesheet.setEmployeeID(employeeID);
			newTimesheet.setStatus(TimesheetStatus.DRAFT);

			List<TimesheetLine> timesheetLines = new ArrayList<>();
			TimesheetLine timesheetLine = new TimesheetLine();
			timesheetLine.setEarningsRateID(ordinaryEarningsRateID);
			List<Double> numUnits = new ArrayList<Double>();
			numUnits.add(2.0);
			numUnits.add(10.0);
			numUnits.add(0.0);
			numUnits.add(0.0);
			numUnits.add(5.0);
			numUnits.add(0.0);
			numUnits.add(5.0);
			timesheetLine.setNumberOfUnits(numUnits);

			//GET Settings
			SettingsObject settings = payrollAuApi.getSettings(access_token, xero_tenant_id);
			Settings s = settings.getSettings();
			SettingsTrackingCategories  s2 = s.getTrackingCategories();
			timesheetLines.add(timesheetLine);
			newTimesheet.setTimesheetLines(timesheetLines);
			newTimesheets.add(newTimesheet);
			Timesheets createdTimesheets = payrollAuApi.createTimesheet(access_token, xero_tenant_id, newTimesheets);
			System.out.println("CREATED  Timesheets - ID:" + createdTimesheets.getTimesheets().get(0).getTimesheetID() );                
			UUID timesheetId = createdTimesheets.getTimesheets().get(0).getTimesheetID();

			// UPDATE timesheet
			Timesheet upTimesheet = new Timesheet();
			List<Timesheet> upTimesheets = new ArrayList<>();

			upTimesheet = createdTimesheets.getTimesheets().get(0);    
			upTimesheet.setStatus(TimesheetStatus.APPROVED);
			upTimesheets.add(upTimesheet);
			Timesheets updatedTimesheets = payrollAuApi.updateTimesheet(access_token, xero_tenant_id, timesheetId, upTimesheets);
			System.out.println(updatedTimesheets.toString());

		} catch (XeroBadRequestException e) {
			System.out.println(Utils.getStringException(e));
		} catch (XeroForbiddenException e) {
			System.out.println(Utils.getStringException(e));
		} catch (XeroNotFoundException e) {
			System.out.println(Utils.getStringException(e));
		} catch (XeroUnauthorizedException e) {
			System.out.println(Utils.getStringException(e)); 
		} catch (XeroMethodNotAllowedException e) {
			System.out.println(Utils.getStringException(e)); 
		} catch (Exception e) {
			System.out.println(Utils.getStringException(e));
		} 

but when i run the code i get the followig :

GET Timesheets - total:11 GET one Timesheets - employee id:b6c19a95-27b0-4163-b4ec-a65949c44bb8 Employee=James Lebron com.xero.api.XeroServerErrorException: An error occurred in Xero. Check the API Status page http://status.developer.xero.com for current service status. at com.xero.api.XeroApiExceptionHandler.execute(XeroApiExceptionHandler.java:211) at com.xero.api.client.PayrollAuApi.createTimesheet(PayrollAuApi.java:780) at au.com.aquabubbler.lucille.xero.XeroTimesheetUploader.process(XeroTimesheetUploader.java:254) at au.com.aquabubbler.lucille.tools.XeroTimesheetUploaderTester.main(XeroTimesheetUploaderTester.java:27) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61) Caused by: com.google.api.client.http.HttpResponseException: 500 Internal Server Error {"Title":"An error occurred","Detail":"An error occurred in Xero. Check the API Status page http://status.developer.xero.com for current service status.","Status":500,"Instance":"1a45aae2-fe1f-4092-ba85-86b392958aeb"} at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1070) at com.xero.api.client.PayrollAuApi.createTimesheetForHttpResponse(PayrollAuApi.java:839) at com.xero.api.client.PayrollAuApi.createTimesheet(PayrollAuApi.java:757)

The error is in the following line:

Timesheets createdTimesheets = payrollAuApi.createTimesheet(access_token, xero_tenant_id, newTimesheets);

everything up to that line worked ok, but somehow creating the timesheet gets me the 500 error. I am using the Demo Company, is it something that i am doing wrong?

thanks

The payload for the POST timesheet call is not quite correct as there is no pay period specified in the payload.

https://developer.xero.com/documentation/api/payrollau/timesheets#post-timesheets