让我们通过 ajax 请求进行聊天验证

Let's chat authentication via ajax request

我已经为我自己的服务器部署了一个 Let's Chat 应用程序。

但是,我不想使用当前构建的原始 Let's Chat Web 应用程序,而是想开发自己的 API。

并且根据Let's Chat wiki

Revoke an API Token

In the top-left dropdown menu:

  • Select "Auth tokens"
  • Click "Revoke token"
  • Choose "Yes". This will delete any previously generated token.

Basic Authentication

Use the API token as the username when authenticating. The password can be set to anything, but it must not be blank (simply because most clients require it).

到目前为止,我已经生成了自己的令牌并尝试发送 GET 请求来检索我在应用程序中拥有的所有房间,但我遇到了一个错误:401 - Unauthorized - 我'我尝试使用 { data: my_token, password: my_random_password } 凭据发送此请求,但没有成功。所以我的主要问题是:我如何使用 ajax 请求通过 Let's Chat API 进行身份验证?

我找不到任何专用于此类任务的 API url / 端点 - 请帮忙。

编辑:

我也试过设置 headers 但还是不行:

$.ajax({
    url: CHAT_URL + 'rooms',
    beforeSend: function(xhr){
        xhr.setRequestHeader('username', 'NTczYzZ1111111111111111111JiMWE3MGUwYThiNzZhYjhmYjFjOWJkOTQ5ZDQ2YjhjNWUyMzkwNmMzYjhjMQ==');
        xhr.setRequestHeader('password', '123qwe');
    }
}).done(function(resp){
    console.log('1');
    console.log(resp);
}).done(function(resp){
    console.log('2');
    console.log(resp);
});

来自该 wiki 页面:

Use the API token as the Bearer token.

这是通过将 header Authentication 设置为值 bearer YOUR_TOKEN_HERE

来完成的

所以,

 xhr.setRequestHeader('Authentication', 'bearer NTczYzZ1111111111111111111JiMWE3MGUwYThiNzZhYjhmYjFjOWJkOTQ5ZDQ2YjhjNWUyMzkwNmMzYjhjMQ==');

如果您想要使用基本身份验证,这就回答了这个问题

How to use Basic Auth with jQuery and AJAX?