Nodejs .Не удается отправить параметры oauth v1 в запросе get с помощью axios. - PullRequest
0 голосов
/ 27 апреля 2020

Я хотел сделать запрос в ADP с autho1.0a. Я мог делать успешные запросы так, как хотел в почтальоне, но не через свое приложение. скриншот почтальона

npm используемый модуль

аналогичный пост

Код, который я пробовал Часть: 1 Генерация подписи

const crypto = require('crypto')
const OAuth = require('oauth-1.0a')

const oauthObj = {};
function hash_function_sha1(base_string, key) {
    return crypto
        .createHmac('sha1', key)
        .update(base_string)
        .digest('base64')
}
oauthObj.getSignature = async payload => {
    const { consumerKey,consumerSecret,apiUrl,method} = payload;
    const oauth = OAuth({
        consumer: { key: `${consumerKey}`, secret: `${consumerSecret}` },
        signature_method: 'HMAC-SHA1',
        hash_function: hash_function_sha1,
    });
    const request_data = {
        url: `${apiUrl}`,
        method: `${method}`
    }
    const token = {}
    // return oauth.toHeader(oauth.authorize(request_data, token));
    console.log('header string-----',oauth.toHeader(oauth.authorize(request_data, token)));
    return oauth.authorize(request_data, token);
 }
module.exports = oauthObj;

Часть 2: Ax ios Вызов

let oauthData=`oauth_consumer_key=${consumerKey}&oauth_signature_method=HMAC-SHA1&oauth_timestamp=${oauthTimestamp}&oauth_nonce=${oauthNonce}&oauth_version=1.0&oauth_signature=${oauthSignature}= HTTP/1.1`;
        const eventData = await axios({
            url:`${apiUrl}?${oauthData}`,
            // url:`${apiUrl}?${oauthHeader.Authorization}`,
            method:'GET',
            headers:{
                // ...oauthHeader,
                'Authorization':'OAuth',
                'Accept': 'application/json',
                // "Authorization": `'OAuth oauth_consumer_key="${consumerKey}", oauth_nonce="${oauthNonce}", oauth_signature="${oauthSignature}", oauth_signature_method="HMAC-SHA1", oauth_timestamp="${oauthTimestamp}", oauth_version="1.0"`
            }
        });

Ожидаемый результат:

{
    "code": "Gone",
    "message": "Event with token 954c183f-26e0-4f9e-b452-c089aaf9842f has already been consumed."
}

Ошибка приема:

response: {
    status: 401,
    statusText: 'Unauthorized',
    headers: {

Что могло пойти не так?

1 Ответ

1 голос
/ 28 апреля 2020

Попробуйте использовать запрос пакет узла oauth опция

request.get(`${apiUrl}?${oauthData}`, {
    oauth: {
        consumer_key: '..',
        consumer_secret: '..',
    },
    headers: {
        Accept: 'application/json'
    },
}, function (err, res, body) {
    console.log(body);
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...