简体   繁体   English

Nodejs.Unable to send oauth v1 params in get request with axios

[英]Nodejs .Unable to send oauth v1 params in get request with axios

I wanted to make a request to ADP with autho1.0a I was able to make successful requests as I wanted in postman but not through my application.我想使用 autho1.0a 向 ADP 发出请求,我能够按照我在 postman 中的要求发出成功的请求,但不是通过我的应用程序。 postman screenshot postman 截图

npm module used使用npm模块

similar post 类似的帖子

Code I tried Part:1 Signature generation我尝试的代码部分: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;

Part 2: Axios Call第 2 部分:Axios 调用

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"`
            }
        });

Expected Result:预期结果:

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

Receiving error:接收错误:

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

What might have gone wrong?可能出了什么问题?

Try using request node package oauth option尝试使用请求节点 package oauth选项

request.get(`${apiUrl}?${oauthData}`, {
    oauth: {
        consumer_key: '..',
        consumer_secret: '..',
    },
    headers: {
        Accept: 'application/json'
    },
}, function (err, res, body) {
    console.log(body);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM