[英]how do I add api token included as a username for each HTTP request i make to an API
I am having difficulties adding my api token to make http requests on the Worksnaps API. 我在添加我的api令牌以在Worksnaps API上发出http请求时遇到了困难。
Here is their documentation explonation but I can't get it to work. 这是他们的文档说明,但是我无法使用。 https://www.worksnaps.com/api_docs/worksnaps_api.html
https://www.worksnaps.com/api_docs/worksnaps_api.html
And here is my code: 这是我的代码:
// 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
};
It always returns status401,which from my knoledge it means Unauthorized. 它总是返回status401,根据我的知识,它表示未经授权。 Any idea?
任何想法?
According to the docs you provided worksnaps uses basic authentication 根据您提供的文档,工作纳使用基本身份验证
Every user has an API token and authentication is managed using HTTP basic authentication.
每个用户都有一个API令牌,并且使用HTTP 基本身份验证来管理身份验证。 In each request the API token has to be included as the username and the password is ignored (that is, only the API token is used for authenticating API requests).
在每个请求中,必须包含API令牌作为用户名,并且密码被忽略 (也就是说,仅API令牌用于验证API请求)。 Example,
例,
curl -H 'Accept: application/xml' -H 'Content-Type: application/xml' \\ -u hy192jfeh26uiew8yg43mfekb21jfenaxop912f3:ignored -d '...'
curl -H'接受:application / xml'-H'内容类型:application / xml'\\ -u hy192jfeh26uiew8yg43mfekb21jfenaxop912f3:ignored -d'...'
so what you're looking for is: 所以您要寻找的是:
var auth_hash = new Buffer(token_string + ":ignored").toString('base64')
'Authorization': 'Basic ' + auth_hash
Based on RFC 6750, 2.1. 基于RFC 6750,2.1。 Authorization Request Header Field you probably need to add "Bearer" to your header:
您可能需要在标题的“授权请求标头”字段中添加“ Bearer”:
'Authorization': 'Bearer ' + token_string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.