繁体   English   中英

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

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

我在添加我的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'接受:application / xml'-H'内容类型:application / xml'\\ -u hy192jfeh26uiew8yg43mfekb21jfenaxop912f3:ignored -d'...'

所以您要寻找的是:

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

基于RFC 6750,2.1。 您可能需要在标题的“授权请求标头”字段中添加“ Bearer”:

'Authorization': 'Bearer ' + token_string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM