简体   繁体   English

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

[英]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.

相关问题 首次使用API​​:如何从Quote API构造对正确数据的请求? 包含的链接 - First time using an API: How do I construct a request for the proper data from a quote API? Links included 如何使我的PHP API将JSON返回到Javascript HTTP POST / GET请求? - How do I make my PHP API return JSON to a Javascript HTTP POST/GET request? 如何使此请求 API 与 CORS 一起使用? - How do I make this Request API work with CORS? 如何使用库币 API 发出 Javascript POST 请求? - How do I make a Javascript POST request with the KuCoin API? 如何向需要密码的 api 发出 http 请求,而无需在我的 javascript 代码中写入密码? - How do I make a http request to an api which needs an password without writing my password in my javascript code? 如何对bitstamp发出HTTP请求? - How do I make an HTTP request to bitstamp? Google API-如何使HTTP Get来对用户进行身份验证并从客户端js获取刷新令牌? - Google API - How can I make a HTTP Get to authenticate user and get a refresh token from client js? 如何在 javascript 中提出 api 请求 - How can I make api request in javascript 具有用户名/ API令牌javascript的Jenkins POST请求 - Jenkins POST Request with username/API Token javascript jQuery HTTP如何将请求发布到API? - How to do a jquery http post request to an API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM