繁体   English   中英

Python 请求和 Bonanza API Node.js / 无法正确发出 API 请求 / 没有 Node.js 文档

[英]Python Requests and Bonanza API Node.js / Unable to Properly Make API Requests / No Node.js Documentation

我正在关注Bonanza.com Bonapitit 文档并成功地让他们的 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. 你应该如何“命名你的请求”?

如果您能在我的脚本中发现问题,请回复。

感谢@rdas,我通过以下方式解决了它...

let headers = {
      'X-BONANZLE-API-DEV-NAME' : bonanza_devId,
      'X-BONANZLE-API-CERT-NAME' : bonanza_certId,
      Accept: "application/json",
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  }
let url = `https://api.bonanza.com/api_requests/secure_request`
let response = await fetch(url, {
      method: "POST",
      headers: headers,
      body: encodeURIComponent("fetchTokenRequest")
    })

有了这个,我成功地获得了令牌。

这让一切变得不同... encodeURIComponent("fetchTokenRequest")

我怀疑他们所有的 API 调用都可以通过这种方式在 NodeJS 中进行。

暂无
暂无

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

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