Metadeta error on stripe transaction using python - python

I am doing stripe payment integration using python and use the following data:
import requests
import json
pos = requests.post
url = "https://api.stripe.com/v1/sources"
headers = {'AUTHORIZATION': 'Bearer sk_test_NXht3wZpuYWRIWpMDDqT3RG2'}
data = {
'type': 'alipay',
'owner[email]': 'abc#xyz.com',
'redirect[return_url]': 'https://www.google.com',
'amount': '500',
'currency': 'USD',
'metadata': {
'data': 'data'
}
}
pos(url, data=data, headers=headers).text
json.loads(pos(url, data=data, headers=headers).text)
When give the metadata it gives error '{\n "error": {\n "message": "Invalid hash",\n "param": "metadata",\n "type": "invalid_request_error"\n }\n}\n'
but according to stripe documentation metadata can be used( https://stripe.com/docs/api/curl#create_source-metadata)
Can anyone tell the solution why it gives that error.

This will solve the problem.
import requests
import json
pos = requests.post
url = "https://api.stripe.com/v1/sources"
headers = {'AUTHORIZATION': 'Bearer sk_test_NXht3wZpuYWRIWpMDDqT3RG2'}
data = {
'type': 'alipay',
'owner[email]': 'abc#xyz.com',
'redirect[return_url]': 'https://www.google.com',
'amount': '500',
'currency': 'USD',
'metadata[data]': 'data'
}
pos(url, data=data, headers=headers).text
json.loads(pos(url, data=data, headers=headers).text)

Stripe does not support JSON payloads for the parameters. Instead, they require application/x-www-form-urlencoded.
At the moment, you are sending metadata as a hash and you are not encoding it properly so Stripe is rejecting it.
The best solution here is to avoid doing this yourself and instead rely on Stripe's official Python library that you can find here: https://github.com/stripe/stripe-python

Related

Baselinker api GET method

I have a problem with code. I try to get list of products and stocks from Baselinker. I receive 200 response but unfortunately also have response:
{'status': 'ERROR', 'error_code': 'ERROR_UNKNOWN_METHOD',
'error_message': 'An empty or unknown method has been used'}
I think I made mistake with methods definition but I try everything I can and problem still exist.
import requests
import json
import webbrowser
from pprint import pprint
params = {
"inventory_id": "1762"
}
parameters = json.dumps(params)
headers = {
"X-BLToken" : token,
'method': "getInventoryProductsStock",
'parameters': parameters
}
response = requests.get('https://api.baselinker.com/connector.php', headers=headers)
print(response)
show = response.json()
print(show)
Parameters should not be placed in a header. You should send a POST request (not a GET) with data, containing 'method' key and optionally 'parameters'.
Try this:
import json
import requests
data = {
"method": "getInventoryProductsStock"
"parameters": json.dumps({"inventory_id": 1762})
}
headers = {
"X-BLToken" : token,
}
response = requests.post('https://api.baselinker.com/connector.php', headers=headers, data=data)
print(response)
show = response.json()
print(show)

Xero API not working for Accounts in python

I am trying to call xero Accounts api in python but not working for some reason. It works well for the rest of APIs such as Invoices, reports, and so on, but Accounts.
The following script is my python script i am using to call the api.
get_url = 'https://api.xero.com/api.xro/2.0/Accounts'
response = requests.get(get_url,
headers = {
'Authorization': 'Bearer ' + access_token,
'Xero-tenant-id': xero_tenant_id,
'Accept': 'application/json'
})
json_response = response.json()
print(json_response)
But this is throwing the following error:
{'Type': None, 'Title': 'Unauthorized', 'Status': 401, 'Detail': 'AuthorizationUnsuccessful', 'Instance': '3c1649ef-6eed-4e64-8503-04fc99481db2', 'Extensions': {}}
Can anyone tell me why this is happening? why just Accounts?
Can you share what scopes you're requesting? https://developer.xero.com/documentation/oauth2/scopes
Invoices requires accounting.transactions
Reports requires accounting.reports.read
Accounts requires accounting.settings

Trouble getting contact folders through microsoft graph api

I'm having difficulties getting Microsoft Graph to return two test Contact Folders that I have set up named Test and Test 2.
When I use v1.0:
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json'}
url = 'https://graph.microsoft.com/v1.0/me/contactFolders'
response = requests.get(url,headers=headers)
response_data = response.json()
print(response_data)
I get a blank value in the response:
{
'#odata.context': "https://graph.microsoft.com/v1.0/$metadata#users('jacobdansey%40hotmail.com')/contactFolders",
'value': []
}
When I use the Beta, I get this which at least returns something but not what I'm looking for:
{
'#odata.context': "https://graph.microsoft.com/beta/$metadata#users('jacobdansey%40hotmail.com')/contactFolders",
'value': [{
'id': '*ID*',
'parentFolderId': '*ParentID*',
'displayName': 'Contacts',
'wellKnownName': 'contacts'
}, {
'id': '*ID*',
'parentFolderId': '*ParentID*',
'displayName': 'Skype Contacts',
'wellKnownName': 'skypecontacts'
}]
}
I know I am connecting properly because when I ask for just contacts from https://graph.microsoft.com/v1.0/me/contacts, it returns the correct answer.
Any help would be greatly appreciated, thanks!
EDIT: Is there a difference between contact folders and contact lists?
i am assuming you want access to folder to get the contacts, if thats the case then you can directly get the contacts of that folder changing the get url
https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list_contacts

Facebook Messenger Bot Python Send Image

I'm trying to send an image with a facebook bot. I can send text fine, but whenever I try to send a message I get an error: TypeError: open file 'plot.jpg', mode 'rb' at 0x7f34a2fe8b70 is not JSON serializable. I'm using flask and heroku for the bot if that makes a difference.
This is my code:
def send_message(recipient_id, message_text):
log("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"]
}
headers = {
"Content-Type": "application/json"
}
log(os.getcwd())
data = json.dumps({
'recipient': {
'id': recipient_id
},
'message': {
'attachment': {
'type': 'image',
'payload': {}
}
},
'filedata': (os.path.basename('plot.jpg'), open('plot.jpg', 'rb'))
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
log(r.status_code)
log(r.text)
I got the same issue and i solved it by using multipart/form-data, instead of encoding the entire payload using json.dumps() you can use the MultipartEncoder from requests-toolbelt-0.8.0 to multipart encode the payload.
Note - Facebook's Graph API is accepting only png images for some unknown reasons, in the below example i've used a png file.
*Edited the code(redundant end parenthesis)
import json
import requests
from requests_toolbelt import MultipartEncoder
def send_message(recipient_id, message_text):
log("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"]
}
log(os.getcwd())
data = {
# encode nested json to avoid errors during multipart encoding process
'recipient': json.dumps({
'id': recipient_id
}),
# encode nested json to avoid errors during multipart encoding process
'message': json.dumps({
'attachment': {
'type': 'image',
'payload': {}
}
}),
'filedata': (os.path.basename('plot.png'), open('plot.png', 'rb'), 'image/png')
}
# multipart encode the entire payload
multipart_data = MultipartEncoder(data)
# multipart header from multipart_data
multipart_header = {
'Content-Type': multipart_data.content_type
}
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=multipart_header, data=multipart_data)
if r.status_code != 200:
log(r.status_code)
log(r.text)

Adding users to custom audiences error - An unknown error has occurred

I try to add users to custom audiences with this code:
{
import requests
import json
payload = {'data': ['33f6fc8e08b0804555feeed0e0e81251bc408c7db58c7a030a8252731668afd0'],
'schema': 'EMAIL_SHA256'}
params = {'access_token': 'ACCESSTOKEN'}
response = requests.post('https://graph.facebook.com/audience_id/users',
params=params, data=json.dumps(payload))
response.json()
}
The response is:
{
{u'error': {u'code': 1,
u'message': u'An unknown error has occurred.',
u'type': u'OAuthException'}}
}
It's interesting that even the original example with curl returns the same result however the access token is valid and I can get data about audiences or create a new one with Python code.
Why I get this error?
The solution for my problem:
{
import requests
import json
payload = {'data': ['33f6fc8e08b0804555feeed0e0e81251bc408c7db58c7a030a8252731668afd0'],
'schema': 'EMAIL_SHA256'}
params = {'access_token': 'ACCESSTOKEN', 'payload': json.dumps(payload)}
response = requests.post('https://graph.facebook.com/audience_id/users',
params=params)
response.json()
}

Categories

Resources