django Safaricom达拉贾:检测到重复的json密钥

5uzkadbs  于 2022-12-24  发布在  Go
关注(0)|答案(3)|浏览(139)

我在Safaricom上实现了利帕na M-Pesa,但是,当我请求时,我收到一个错误消息,说检测到重复的json密钥。
有效载荷:

payload = {
    "BusinessShortCode": 174379, 
    "Password": "the password", 
    "Timestamp": "20220817050503", 
    "TransactionType": "CustomerPayBillOnline", 
    "Amount": 5, 
    "PartyA": 254714895622, 
    "PartyB": 174379, 
    "PhoneNumber": 254714895622, 
    "CallBackURL": "https://58fa-102-68-78-50.in.ngrok.io/api/v1/payments/callback/", 
    "AccountReference": "Payment", 
    "TransactionDesc": "Payment for K"
}

标题:

headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer access_token",
}

请求:

response = requests.request("POST", 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest', headers=headers, data=payload)

我得到的回答是:

{
   'responseId': '82699-21939686', 
   'responseCode': '400', 
   'responseDesc': 'Duplicate json key detected'
}

这个错误的原因可能是什么?我该如何解决它?

jdgnovmf

jdgnovmf1#

问题出在标头中,请求默认传递Content-Type,删除它修复了问题。

8gsdolmq

8gsdolmq2#

虽然在Safaricom的Python的M-pesa Daraja代码示例中提供了它,但请尝试更改此部分以使其成为;将body=payload重命名为json=payload

response = requests.request("POST",your-url, headers=headers, json=payload)

代替

response = requests.request("POST", url , headers=headers, data=payload)

按照建议从头文件中删除Content-Type给了我另一个错误;

{'requestId': '12345-12345-1',
'errorCode': '400.002.02',
'errorMessage': 'Bad Request - Invalid BusinessShortCode'}

对我有用,希望对你有用

myss37ts

myss37ts3#

以下工程

import requests

headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer DWbh63zkq7ZLagmNCSwnyvjA2kBQ'
}

payload = {
    "BusinessShortCode": 174379,
    "Password": "MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMjIxMjIyMDUyOTQ5",
    "Timestamp": "20221222052949",
    "TransactionType": "CustomerPayBillOnline",
    "Amount": 1,
    "PartyA": 254712345678,
    "PartyB": 174379,
    "PhoneNumber": 254712345678,
    "CallBackURL": "example.com/path",
    "AccountReference": "CompanyXLTD",
    "TransactionDesc": "Payment of X" 
  }

response = requests.request("POST", 'example.com/mpesa/stkpush/v1/processrequest', headers = headers, json = payload)
print(response.text.encode('utf8'))

相关问题