I was able to figure out a solution with Axios. I created an OauthHelper class to generate the Authorization
header:
const crypto = require('crypto');
const oauth1a = require('oauth-1.0a');
const CONSUMERKEY = '<consumerKey>';
const CONSUMERSECRET = '<consumerSecret>';
const TOKENKEY = '<tokenKey>';
const TOKENSECRET = '<tokenSecret>';
class Oauth1Helper {
static getAuthHeaderForRequest(request) {
const oauth = oauth1a({
consumer: { key: CONSUMERKEY, secret: CONSUMERSECRET },
signature_method: 'HMAC-SHA1',
hash_function(base_string, key) {
return crypto
.createHmac('sha1', key)
.update(base_string)
.digest('base64')
},
})
const authorization = oauth.authorize(request, {
key: TOKENKEY,
secret: TOKENSECRET,
});
return oauth.toHeader(authorization);
}
}
module.exports = Oauth1Helper;
Then I was just able to make the post from wherever I need via Axios:
const request = {
url: 'https://api-domain.com',
method: 'POST',
body: {
"uniqueId": 1234
}
};
const authHeader = Oauth1Helper.getAuthHeaderForRequest(request);
return await axios.post(
request.url,
request.body,
{ headers: authHeader });
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…