Search code examples
restapiclockify

Clockify API auth


I am trying to follow your steps with connecting to your API, but I am really not sure how to even start the auth... First attempt was making Javascript communication via AJAX, so here is the code for it:

$.ajax({
    url: 'https://api.clockify.me/api/auth/token/',
    method: 'POST',
    cache: false,
    contentType: 'application/json',
    headers: {
        'X-Api-Key': 'MyAPIKey'
    },
    data: { "email": "MyMail", "password": "MyPass" },
    always: function(r){
        console.log(r);
    }
});

constantly on response I get error like this:

{"message":"Could not read document: Unrecognized token 'email': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@5cfd6511; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'email': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@5cfd6511; line: 1, column: 7]","code":3002}

Next, I tried to move on and start communication from PHP and using CURL to do auth and everything, but I keep bumping on same error / problem.

Is there anything I am missing maybe?


Solution

    1. You don't need to use X-Api-Key to obtain authentication token.
    2. It seems like JQuery wants data to be serialized before sending, so simply use JSON.stringify like in this example:
    $.ajax({
      url: 'https://api.clockify.me/api/auth/token/',
      method: 'POST',
      contentType: "application/json",
      data: JSON.stringify({
        email: "[email protected]",
        password: "secretpass"
      }),
      always: function(r) {
        console.log(r);
      }
    });