OAuth2 integration with ExactOnline

1.3k views Asked by At

I am trying to integrate our app with Exact Online website using OAuth2,or more specifically i am trying to create hour registration which should include "Employee","Project","Hours","Hours type".

function registerTime($access_token_for_data) {

    //$dataToFilterAccounts = array('$filter' => 'IsSales eq true');
    $dataToRetrieveFromEmployees = array('$select' => 'ID');
    // $queryToFilterAccounts = http_build_query($dataToFilterAccounts);   
    $queryToRetrieveFromEmployees = http_build_query($dataToRetrieveFromEmployees);

    // $dataToFilterItems = array('$filter' =>'IsSalesItem eq true');
    $dataToRetrieveProjects = array('$select' => 'ID');
    //$queryToFilterItems = http_build_query($dataToFilterItems);   
    $queryToRetrieveProjects = http_build_query($dataToRetrieveProjects);

    $urlProjects = 'https://start.exactonline.nl/api/v1/638842/project/Projects';
    $urlEmployees = 'https://start.exactonline.nl/api/v1/638842/payroll/Employees';

    $curlProjects = curl_init($urlProjects);
    $curlEmployees = curl_init($urlEmployees);

    $headers = array(
        'Authorization: Bearer ' . $access_token_for_data,
        'Accept: application/json',
        'Content-type: application/json'
    );

    curl_setopt($curlProjects, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curlProjects, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlProjects, CURLOPT_URL, $urlProjects . '?' . $queryToRetrieveProjects);
    curl_setopt($curlProjects, CURLOPT_CUSTOMREQUEST, 'GET');

    curl_setopt($curlEmployees, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curlEmployees, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlEmployees, CURLOPT_URL, $urlEmployees . '?' . $queryToRetrieveFromEmployees);
    curl_setopt($curlEmployees, CURLOPT_CUSTOMREQUEST, 'GET');


    $resultProjects = curl_exec($curlProjects);
    $resultEmployees = curl_exec($curlEmployees);

    $projectsData = json_decode($resultProjects, true);
    $projectID = $projectsData["d"]["results"]["0"]["ID"];

    $employeesData = json_decode($resultEmployees, true);
    $employeeID = $employeesData["d"]["results"]["0"]["ID"];

    curl_close($curlProjects);
    curl_close($curlEmployees);

    $urlTimeTransaction = 'https://start.exactonline.nl/api/v1/638842/project/TimeTransactions';

    $curlTimeTransacation = curl_init($urlTimeTransaction);
    $content = json_encode(array("Project" => $projectID, "Employee" => $employeeI));

    curl_setopt($curlTimeTransacation, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curlTimeTransacation, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlTimeTransacation, CURLOPT_POST, true);
    curl_setopt($curlTimeTransacation, CURLOPT_POSTFIELDS, $content);

    $createdTimeTransaction = curl_exec($curlTimeTransacation);

    $status = curl_getinfo($curlTimeTransacation, CURLINFO_HTTP_CODE);
    if ($status != 201) {
        die("Error: call to URL $curlTimeTransacation failed with status $status, response $createdTimeTransaction, curl_error " . curl_error($curlTimeTransacation) . ", curl_errno " . curl_errno($curlTimeTransacation));
    }
    echo "HTTP status $status creating time registartion<br/><br/>";
    curl_close($curlTimeTransacation);
}

And this is the error i get

Error: call to URL Resource id #56 failed with status 500, response { "error": { "code": "", "message": { "lang": "", "value": "Mandatory: Employee\r\nMandatory: Hours\r\nMandatory: Hour type" } } }, curl_error , curl_errno 0

But when i try to include any of these mandatroy fileds i get:

Error: call to URL Resource id #56 failed with status 400, response { "error": { "code": "", "message": { "lang": "", "value": "Error processing request stream. The property name 'Hours' specified for type 'Exact.Web.Api.Models.TimeTransaction' is not valid." } } }, curl_error , curl_errno 0

2

There are 2 answers

0
Patrick Hofman On

As Guido pointed out correctly, there is no field named Hours in the TimeTransaction on projects, there is a field named Hours on the manufacturing TimeTransaction. This is a little confusing, especially since the error message isn't very clear.

You need to set Quantity on TimeTransactions in order to specify the hours on a project's time transaction.

0
Guido Leenders On

please note that /api/v1/638842 contains your division ID. You might want to change that into a variable.

Regarding your problem: please note that the error messages contain text to be consumed by humans. The actual technical names can be different. I always do it the other way around: I query the existing data and look at all fields, and then I know what to send. You can use the Query Tool for Exact Online in the app center of Exact to do the query on REST api of Exact Online (but I am biased because involved).