【发布时间】:2021-09-04 01:35:21
【问题描述】:
我正在关注Bonanza.com Bonapitit documentation 并成功地让他们的 Python fetchToken 脚本工作。但是,我现在正尝试在 Node.js 中重新创建相同的脚本,因为我希望它在我的 ExpressJS 后端(而不是 Python)中。
这里是工作 fetchToken Python 脚本的样子...
import requests
url = 'https://api.bonanza.com/api_requests/secure_request'
# See your Bonapitit welcome e-mail or your Bonapitit dashboard for
# your developer and certificate identifiers
headers = {
'X-BONANZLE-API-DEV-NAME' : 'my_dev_name',
'X-BONANZLE-API-CERT-NAME' : 'my_cert_name'
}
request_name = 'fetchTokenRequest'
# make the request to Bonanza
response = requests.post(url, data=request_name, headers=headers)
# the json response as a dictionary
response_json = response.json()
# examine the response
if response_json['ack'] == 'Success' and 'fetchTokenResponse' in response_json:
token_response = response_json['fetchTokenResponse']
print("Your token: " + token_response['authToken'])
print("Token expiration time: " + token_response['hardExpirationTime'])
print("Authentication URL: " + token_response['authenticationURL'])
else:
print(response_json)
所以我想,好吧,这应该很容易转换为 JavaScript,所以这就是我尝试的...
exports.fetchToken = async (req, res) => {
let _id = req.params.userId
let bonanza_devId
let bonanza_certId
try {
let user = await User.findById({_id})
bonanza_devId = user.bonanza_devId
bonanza_certId = user.bonanza_certId
} catch (e) {
console.log(`Got an error when obtaining the user data in fetchToken...`, e)
}
try {
let request_name = 'fetchTokenRequest'
let headers = {
'X-BONANZLE-API-DEV-NAME' : bonanza_devId,
'X-BONANZLE-API-CERT-NAME' : bonanza_certId,
Accept: "application/json",
"Content-Type": "application/json",
request_name: request_name
}
let url = `https://api.bonanza.com/api_requests/secure_request`
let response = await fetch(url, {
method: "POST",
headers: headers,
body: new URLSearchParams({
request_name
})
})
response = await response.json()
res.json(response)
} catch (e) {
res.json({error: e})
}
}
(上面的脚本正在从数据库中获取用户,从中获取 api 凭据,然后尝试进行调用)
不幸的是,我在使用 JavaScript 版本时收到此错误...
{
"ack": "Failure",
"version": "1.1.2",
"timestamp": "2021-06-19T18:53:00.000Z",
"errorMessage": {
"message": "The request was not formatted correctly",
"type": "Bonapitit::InvalidRequestFormat"
}
}
我认为 request_name = 'fetchTokenRequest' 没有被错误地放入脚本中,因为在 Python 脚本中它使用了 data=request_name。
在 JavaScript fetch 中与 `data=request_name 等效的是什么?
此外,当我尝试将 Content-Type 从 "Content-Type": "application/json" 更改为 "Content-Type": "application/x-www-form-urlencoded" 时,它会将错误消息更改为此...
{
"ack": "Failure",
"version": "1.1.2",
"timestamp": "2021-06-19T19:04:54.000Z",
"errorMessage": {
"message": "Bonapitit::CannotFindRequest",
"type": "Bonapitit::CannotFindRequest"
}
}
在文档中,它声明 The name for your request should be fetchTokenRequest. 你应该如何这样“命名你的请求”?
如果您能在我的脚本中发现问题,请回复。
【问题讨论】:
-
在
requests中使用data将内容类型设置为"application/x-www-form-urlencoded"。数据本身就是请求的整个主体(URL 编码)。因此,在您的 JS 脚本中,您只需将字符串"fetchTokenRequest"作为正文(url 编码)传递 -
@rdas 我爱你!这完全为我解决了。谢谢。
标签: javascript python node.js python-3.x python-requests