gpt4 book ai didi

node.js - 如何使用 set 2Checkout Authentication Headers?

转载 作者:行者123 更新时间:2023-12-03 12:13:08 27 4
gpt4 key购买 nike

我正在尝试使用 2checkout REST API

https://knowledgecenter.2checkout.com/API-Integration/REST_5.0_Reference#/introduction/authentication/json-encoded-requests

这是我如何尝试请求的片段

const axios = require('axios')
const moment = require('moment')
const saltedMd5 = require('salted-md5');

let now = moment().format('YYYY-MM-DD HH:MM:SS')
let vendorCode = '250207358831'

let toHash = vendorCode.length + vendorCode + now.length + now

let salt = '~0CSl)!M@4rZ|zX5QR&s'
const hash = saltedMd5(toHash, salt)

axios.get('https://api.2checkout.com/rest/5.0/subscriptions/?Email=customer%40email.com&AvangateCustomerReference=1234567&ExternalCustomerReference=abcdefg&Page=1&Limit=10&PurchasedBefore=2015-12-29&PurchasedAfter=2015-01-15&ExpireBefore=2016-05-22&ExpireAfter=2015-07-23&Type=regular&Aggregate=false', {
headers: {
'X-Avangate-Authentication': `code="${vendorCode}" date="${now}" hash="${hash}"`,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})

它返回状态代码 500。有人知道如何使用 2checkout API 检索订阅吗?

最佳答案

class TwoCheckoutService {
tco: {
domain:string;
apiUrl: string,
apiUser:string,
apiPass:string,
sellerId:string,
privateKey:string,
secretKey:string,
demo:boolean,
};

constructor(private userService: UserService) {
this.tco = {=
apiUrl: 'https://api.2checkout.com/rest/6.0',
apiUser: "=",
apiPass: "=",
sellerId: "=",
privateKey: "=",
secretKey: "=",
demo: true,
// sandbox: false
};
}

private async _getAuthHeaders(): Promise<{[key:string]: string}> {

var code = this.tco.sellerId;
var date = moment().utc().format('YYYY-MM-DD hh:mm:ss');
var stringToHash = code.toString().length + code + date.toString().length + date;
var hmac = crypto.createHmac('md5', this.tco.secretKey);
hmac.update(stringToHash, 'utf8');

var hash = hmac.digest('hex')
var authHeader = `code="${code}" date="${date}" hash="${hash}"`

return {
'X-Avangate-Authentication': authHeader,
'Content-Type': 'application/json',
'Accept': 'application/json'
};
}

async getProducts() {
var url = this.tco.apiUrl + '/products/';
var headers = await this._getAuthHeaders();
console.log(headers);
var res = await Axios.get(url, {
headers: headers,
params: {
'Limit': 10,
'Page': 1,
},
validateStatus: (status)=>true
});

if(res.status === 200) return res.data;
return {
error: true,
data: res.data,
url: url,
headers: headers
}
}
}
#这是 typescript nodejs中的一个例子
##要求
  • 加密
  • axios
  • 2checkout api 凭证
  • 关于node.js - 如何使用 set 2Checkout Authentication Headers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57951960/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com