zadarma sms send POST error 401 Not authorized

node v10.23.3
version: master
endpointsharetweet
const crypto = require('crypto'); const fetch = require('node-fetch'); const Bluebird = require('bluebird'); fetch.Promise = Bluebird; const userKey = "b46b5676866e01003bab"; const userSecret = "938b9c6d4bb7d2e262a4"; const prepare = (method, params) => { let params_str = Object.keys(params) .sort((a, b) => a === b ? 0 : a > b ? 1 : -1) .map(key => `${key}=${params[key]}`) .join('&'); let md5 = crypto.createHash('md5').update(params_str).digest('hex'); let hex = crypto.createHmac('sha1', userSecret) .update(method + params_str + md5).digest('hex'); let sign = new Buffer.from(hex).toString('base64'); return { authorization: userKey + ":" + sign, url: "https://api.zadarma.com" + method + "?" + params_str } } const fetch_request = async ({url, method, params, authorization}) => { let opt = { method: method, headers: { "Authorization": authorization, 'Content-Type': 'application/json' } } if(method === 'POST'){ opt.body = JSON.stringify(params) } let res = await fetch(url, opt) .then(res => res.json()) .catch(err => {console.log(err)}); return res; } let send_to_api = async ({get_post, method, params}) => { let {url, authorization} = prepare(method, params); return fetch_request({ url: url, method: get_post, params: params, authorization: authorization }); } (async _ => { let result = await send_to_api({ get_post: 'GET', method: '/v1/info/price/', params: { 'number': encodeURI('380959897908'), 'caller_id': encodeURI('380948232794') } }); console.log('----/v1/info/price/', result); result = await send_to_api({ get_post: 'POST', method: '/v1/sms/send/', params: { 'number': encodeURI('380959897908'), 'message': encodeURI('test тест 123') } }); console.log('------/v1/sms/send/', result); })()
Created from: https://npm.runkit.com/url
zadarma sms send POST error 401 Not authorized
Loading…

1 comment

  • posted a year ago by ssplast
    Не удаётся авторизоваться при POST запросе. GET запрос с параметрами, всё OK? а вот POST не принимает.

sign in to comment