首页 > 解决方案 > Clockify API 身份验证

问题描述

我正在尝试按照您的步骤连接到您的 API,但我真的不知道如何启动身份验证...第一次尝试是通过 AJAX 进行 Javascript 通信,所以这里是它的代码:

$.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);
    }
});

不断响应我得到这样的错误:

{"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}

接下来,我尝试继续从 PHP 开始通信并使用 CURL 进行身份验证和所有操作,但我一直遇到相同的错误/问题。

有什么我想念的吗?

标签: restapiclockify

解决方案


  1. 您不需要X-Api-Key用于获取身份验证令牌。
  2. 看起来 JQuery 想要在发送之前对数据进行序列化,所以只需JSON.stringify在这个例子中使用:
$.ajax({
  url: 'https://api.clockify.me/api/auth/token/',
  method: 'POST',
  contentType: "application/json",
  data: JSON.stringify({
    email: "someone@example.com",
    password: "secretpass"
  }),
  always: function(r) {
    console.log(r);
  }
});

推荐阅读