HTTP 400 on payout - python

I've been trying to build Paypal cash out system for my website, I viewed countless Paypal API pages and interpreted all the examples to Python:
import requests
import string
import random
import json
from requests.auth import HTTPBasicAuth
class makePayment(object):
def __init__(self, paypal_email, subject, amount, note, clientid, secret):
self.access_token = self.obtain_access_token(clientid, secret) # Successfully obtained access token
self.headers = {"Content-type": "application/json", "Authorization": "Bearer {0}".format(self.access_token)}
self.sender_batch_id = self.generate_sender_batch_id() # Randomly generated sender_batch_id in base36 ([A-Z] and [1-9])
self.payout_form = self.create_payout_form(self.sender_batch_id, paypal_email, subject, amount, note) # Not encoded in JSON, just normal HTML form
self.response = requests.post("https://api.sandbox.paypal.com/v1/payments/payouts",
data=self.payout_json,
headers=self.headers)
def httpResponse(self):
return self.response
#staticmethod
def obtain_access_token(clientid, secret):
headers = {"Accept": "application/json", "Accept-Language": "en_US"}
response = requests.post("https://api.sandbox.paypal.com/v1/oauth2/token",
auth=HTTPBasicAuth(clientid, secret),
data={"grant_type": "client_credentials"},
headers=headers)
return dict(response.json()).get("access_token")
#staticmethod
def generate_sender_batch_id():
base36 = str(string.digits + string.ascii_lowercase)
return "".join([random.choice(base36) for char in range(1, 9)])
#staticmethod
def create_payout_form(sender_batch_id, paypal_email, subject, amount, note):
payout_dict = {
'sender_batch_header': {
'sender_batch_id': sender_batch_id,
'email_subject': subject,
},
'items': [{
'recipient_type': 'EMAIL',
'amount': {
'value': amount,
'currency': 'USD'
},
'receiver': paypal_email,
'note': note,
'sender_item_id': '${0} item'.format(amount),
}]
}
return payout_dict
token generator, payout example
I get http error 400 (bad request), before that i've tried encoding data in json and got http error 422 (UNPROCESSABLE ENTITY), maybe json encoding was the proper method?
Take in note that sender_batch_id is randomly generated, since i don't know any other method to obtain it.
What am i doing incorrectly? Is there a possibility that i'm missing some parameters? I think there's some problem with the authentication header. Thanks!

most of the times - POST requests that are from type "application/json" need to be sent in a json format :
response = requests.post("https://api.sandbox.paypal.com/v1/oauth2/token",
auth=HTTPBasicAuth(clientid, secret),
json={"grant_type": "client_credentials"},
headers=headers)
i used
json
instead of
data
in your POST request

Related

python requests not recognizing params

I am requesting to mindbodyapi to get token with the following code using requests library
def get_staff_token(request):
URL = "https://api.mindbodyonline.com/public/v6/usertoken/issue"
payload = {
'Api-Key': API_KEY,
'SiteId': "1111111",
'Username': 'user#xyz.com',
'Password': 'xxxxxxxx',
}
r = requests.post(url=URL, params=payload)
print(r.text)
return HttpResponse('Done')
gives a response as follows
{"Error":{"Message":"Missing API key","Code":"DeniedAccess"}}
But if I request the following way it works, anybody could tell me, what I am doing wrong on the above code.
conn = http.client.HTTPSConnection("api.mindbodyonline.com")
payload = "{\r\n\t\"Username\": \"username\",\r\n\t\"Password\": \"xxxxx\"\r\n}"
headers = {
'Content-Type': "application/json",
'Api-Key': API_KEY,
'SiteId': site_id,
}
conn.request("POST", "/public/v6/usertoken/issue", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
In the second one, you are passing the API Key in headers and the credentials in the body of the request. In the first, you are sending both the API Key and credentials together in the query string, not the request body. Refer to requests.request() docs
Just use two dictionaries like in your second code and the correct keywords, I think it should work:
def get_staff_token(request):
URL = "https://api.mindbodyonline.com/public/v6/usertoken/issue"
payload = {
'Username': 'user#xyz.com',
'Password': 'xxxxxxxx',
}
headers = {
'Content-Type': "application/json",
'Api-Key': API_KEY,
'SiteId': "1111111",
}
r = requests.post(url=URL, data=payload, headers=headers)
print(r.text)
return HttpResponse('Done')

Kuna Code activation error by API, how to solve?

Good day!
I am trying to use the API of the Kuna.io exchange
There is a method in the documentation: /v3/auth/kuna_codes/redeem
I get the error:
{"messages": ["signature_is_incorrect"]}
Works correctly with other methods
import requests
import time
import hmac
import hashlib
url = "https://api.kuna.io/v3/auth/kuna_codes/redeem"
api_path = "/v3/auth/kuna_codes/redeem"
secret_key = 'key'
public_key = 'key'
nonce = str(int(time.time()*1000.0))
body = str('')
msg = api_path+nonce+body
kun_signature = hmac.new(secret_key.encode('ascii'), msg.encode('ascii'), hashlib.sha384).hexdigest()
payload = {"code": "ZC7Xr-TBcfa-DW3hg-xNUr8-cxnp2-CHada-QT9Yr-L14DZ-5pyjA-UAH-KCode"}
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
'kun-nonce': nonce,
'kun-apikey': public_key,
'kun-signature': kun_signature,
}
response = requests.request("PUT", url, json=payload, headers=headers)
print(response.text)
My suspicions are that the method is wrong since the signature works correctly with other methods.
The signature should contain a body. Try with this one
payload = {"code": "ZC7Xr-TBcfa-DW3hg-xNUr8-cxnp2-CHada-QT9Yr-L14DZ-5pyjA-UAH-KCode"}
body = json.dumps(payload)
msg = api_path+nonce+body

Retrieving single json object from python requests results as list

I'm trying to retrieve only requestId object from json results after querying my API.
my code:
def findRequests(group, token, user):
headers = { 'accept': 'application/json',
'Content-Type': 'application/json',
'token': token,
'user': user}
endpoint = 'requests/find'
body = {'groupIDs': [group], "createdDate": {'operator': "BETWEEN", 'fromDate': "01-APR-2020 00:00:00", 'toDate': "21-APR-2020 00:00:00"}}
r = requests.post(url = host + endpoint, headers = headers, json=body, verify=False)
data = json.loads(r.text)
print (data[0]['requestId'])
json data:
[{'requestId': 2567887, 'requestName': 'Here is a sample name', 'requestSubject': 'sample subject', 'createdDate': '01-APR-2020 14:06:03'}, {'requestId': 7665432,...}]
then I would like to save all the values for requestId object found in results as a list:
myRequestsList = print(findRequests(group, token, user))
However the above will only return a single requestId and not all of the ones that were returned in data variable in def findRequests(group, token, user). What am I doing wrong?
Output:
2567887
None
Desired output:
2567887,
7665432
Could someone help me with this? thanks in advance!
First, you should modify your func:
Then, assign the variable to the func, not the print:
myRequestsList = list(findRequests(group, token, user)))
(!) However, I assume that group,token, user are replaced by other variables.
And finally, to get the output:
for req in myRequestsList:
print(req)
Later edit:
def findRequests(group, token, user):
headers = { 'accept': 'application/json',
'Content-Type': 'application/json',
'token': token,
'user': user}
endpoint = 'requests/find'
body = {'groupIDs': [group], "createdDate": {'operator': "BETWEEN", 'fromDate': "01-APR-2020 00:00:00", 'toDate': "21-APR-2020 00:00:00"}}
r = requests.post(url = host + endpoint, headers = headers, json=body, verify=False)
data = json.loads(r.text)
final_list = []
for each_req in data:
final_list.append(each_req['requestId'])
return final_list
myRequestsList = findRequests(group, token, user)
for each in myRequestsList:
print(each)

How to pass return as a value for argument with adding a string?

I prepared this method
def make_bearer_token(token_url):
body = {
"scope": "ingo_dev",
"client_id": "machine__dev",
"client_secret": "0c3b99",
"username": "test.user",
"password": "xxx",
"grant_type": "pass",
}
response = requests.post(token_url, data=body).json()
bearer_token = response["access_token"]
print({'authorization: ' + 'Bearer ' + str(response)})
return bearer_token
I want to pass that bearer_token to another method with string 'authorization':bearer_token
as a header parameter. The method is looking something like that.
#staticmethod
def post_request(endpoint, file, timeout=30):
response = requests.post(endpoint, files=file_dict, timeout=timeout)
return response
A simple solution would be to run your make_bearer_token() from within your post_request method.
Bearer is a header so make a new headers dict with your Authorization header in it, and pass it as a parameter to requests.post()
def post_request(endpoint, file, timeout=30):
headers = {
"Authorization": "Bearer {}".format(make_bearer_token(endpoint))
}
response = requests.post(endpoint, files=file_dict, headers=headers, timeout=timeout)
return response
Edit: Based on your comment below, you might want to make use of class variables
class Uploader:
def __init__(self, url):
self.headers = {
"Authorization": "Bearer {}".format(self.make_bearer_token(url))
}
def post_request(self, endpoint, file, timeout=30):
response = requests.post(endpoint, files=file_dict, headers=self.headers, timeout=timeout)
return response
Usually response access_token don't have prefix bearer. So you try this way.
import requests
bearer_token = response["access_token"]
hed = {'Authorization': 'Bearer ' + bearer_token}
data = {'key' : 'value'}
url = 'https://next.url.com'
response = requests.post(url, json=data, headers=hed)
print(response)
print(response.json())
Please try this. Let me know you feedback on the same.

Converting list to python array [duplicate]

I need to POST a JSON from a client to a server. I'm using Python 2.7.1 and simplejson. The client is using Requests. The server is CherryPy. I can GET a hard-coded JSON from the server (code not shown), but when I try to POST a JSON to the server, I get "400 Bad Request".
Here is my client code:
data = {'sender': 'Alice',
'receiver': 'Bob',
'message': 'We did it!'}
data_json = simplejson.dumps(data)
payload = {'json_payload': data_json}
r = requests.post("http://localhost:8080", data=payload)
Here is the server code.
class Root(object):
def __init__(self, content):
self.content = content
print self.content # this works
exposed = True
def GET(self):
cherrypy.response.headers['Content-Type'] = 'application/json'
return simplejson.dumps(self.content)
def POST(self):
self.content = simplejson.loads(cherrypy.request.body.read())
Any ideas?
Starting with Requests version 2.4.2, you can use the json= parameter (which takes a dictionary) instead of data= (which takes a string) in the call:
>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
'data': '{"key": "value"}',
'files': {},
'form': {},
'headers': {'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'close',
'Content-Length': '16',
'Content-Type': 'application/json',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
'X-Request-Id': 'xx-xx-xx'},
'json': {'key': 'value'},
'origin': 'x.x.x.x',
'url': 'http://httpbin.org/post'}
It turns out I was missing the header information. The following works:
import requests
url = "http://localhost:8080"
data = {'sender': 'Alice', 'receiver': 'Bob', 'message': 'We did it!'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
From requests 2.4.2 (https://pypi.python.org/pypi/requests), the "json" parameter is supported. No need to specify "Content-Type". So the shorter version:
requests.post('http://httpbin.org/post', json={'test': 'cheers'})
Which parameter between data / json / files you need to use depends on a request header named Content-Type (you can check this through the developer tools of your browser).
When the Content-Type is application/x-www-form-urlencoded, use data=:
requests.post(url, data=json_obj)
When the Content-Type is application/json, you can either just use json= or use data= and set the Content-Type yourself:
requests.post(url, json=json_obj)
requests.post(url, data=jsonstr, headers={"Content-Type":"application/json"})
When the Content-Type is multipart/form-data, it's used to upload files, so use files=:
requests.post(url, files=xxxx)
The better way is:
url = "http://xxx.xxxx.xx"
data = {
"cardno": "6248889874650987",
"systemIdentify": "s08",
"sourceChannel": 12
}
resp = requests.post(url, json=data)
headers = {"charset": "utf-8", "Content-Type": "application/json"}
url = 'http://localhost:PORT_NUM/FILE.php'
r = requests.post(url, json=YOUR_JSON_DATA, headers=headers)
print(r.text)
Works perfectly with python 3.5+
client:
import requests
data = {'sender': 'Alice',
'receiver': 'Bob',
'message': 'We did it!'}
r = requests.post("http://localhost:8080", json={'json_payload': data})
server:
class Root(object):
def __init__(self, content):
self.content = content
print self.content # this works
exposed = True
def GET(self):
cherrypy.response.headers['Content-Type'] = 'application/json'
return simplejson.dumps(self.content)
#cherrypy.tools.json_in()
#cherrypy.tools.json_out()
def POST(self):
self.content = cherrypy.request.json
return {'status': 'success', 'message': 'updated'}
With current requests you can pass in any data structure that dumps to valid JSON , with the json parameter, not just dictionaries (as falsely claimed by the answer by Zeyang Lin).
import requests
r = requests.post('http://httpbin.org/post', json=[1, 2, {"a": 3}])
this is particularly useful if you need to order elements in the response.
I solved it this way:
from flask import Flask, request
from flask_restful import Resource, Api
req = request.json
if not req :
req = request.form
req['value']
It always recommended that we need to have the ability to read the JSON file and parse an object as a request body. We are not going to parse the raw data in the request so the following method will help you to resolve it.
def POST_request():
with open("FILE PATH", "r") as data:
JSON_Body = data.read()
response = requests.post(url="URL", data=JSON_Body)
assert response.status_code == 200

Categories

Resources