如何添加api令牌作为我向api发出的每个HTTP请求的用户名

how do I add api token included as a username for each HTTP request i make to an API

本文关键字:api 用户 请求 HTTP 何添加 添加 令牌      更新时间:2024-05-28

我在添加我的api令牌以在Worksnaps api上进行http请求时遇到困难。

这是他们的文档说明,但我无法使用它。https://www.worksnaps.com/api_docs/worksnaps_api.html

这是我的代码:

// prepare the header
var headers = {
    //'Content-Type' : 'text/xml',
    //'Content-Length' : Buffer.byteLength(jsonObject, 'utf8'),
    'Authorization': 'token'
};
// options for GET
    var options_get = {
        host: 'worksnaps.com', // here only the domain name
        // (no http/https !)
        //port : 443,
        path: '/api/projects/' + project_id + '/user_assignments.xml', // the rest of the url with parameters if needed
        method: 'GET', // do GET
        headers: headers
    };

它总是返回status401,在我看来,这意味着未经授权。知道吗?

根据您提供的文档,工作快照使用基本身份验证

每个用户都有一个API令牌,使用HTTP管理身份验证基本身份验证。在每个请求中都必须包含API令牌由于用户名和密码被忽略(即,只有API令牌用于认证API请求)。示例,

curl-H"Accept:application/xml"-H"内容类型:application/xml"''-u hy192jfeh26uiew8yg43mfekb21jfenaxop912f3:已忽略-d"…"

所以你要找的是:

var auth_hash = new Buffer(token_string + ":ignored").toString('base64')
'Authorization': 'Basic ' + auth_hash

基于RFC 6750,2.1。您可能需要添加"授权请求头字段";Bearer";到您的标题:

'Authorization': 'Bearer ' + token_string