How to delete an attachable using the Quickbooks API? - python

I am struggling to delete an attachable using the Quickbooks API.
I keep getting the error: ""Message":"Error parsing object","Detail":"ObjectParserError: Request Body is Empty","code":"2310","element":""
I wrote the following function (I put hard coded values to make sure that it works properly). The accessToken is definitely accurate as it works properly for other functions.
def deleteAttachable(accessToken):
base_url = 'https://sandbox-quickbooks.api.intuit.com/'
url = '{0}/v3/company/{1}/attachable?operation=delete'.format(base_url, cfg.qBData["realm_id"])
auth_header = 'Bearer {0}'.format(accessToken)
headers = {
"SyncToken": "0",
"domain": "QBO",
'Id': "5000000000001149164",
"FileName" : "bf546e8b-1dc4-4e42-b305-6435c28e2d8a1-AMAZON THERMOSTAT.pdf",
'Authorization': auth_header,
'Content-type': 'application/json'
}
print(url)
print(headers)
response = requests.post(url, headers=headers)

Related

Replicate a Postman POST-Request (metadata and fileupload) within python

I am trying to issue a POST request including local-file upload with python.
I looked up a lot of similar requests here, but none worked for me or none was specific enough to help me out.
With Postman everything works as expected and without any hickups.
But representing the very same with python I am not able to achieve it.
I tried various combinations of params-, body- and file-dictionaries, without success
This is how the working postman request body looks like:
This is how the params of this postman request have been configured:
This is my python code
metadata = {
"name":"MyFile" ,
"type":"myFileType" ,
"parentId":"1cc58622-3bc0-4fc4-a222-a64bd8d90af1"
}
fileForUpload = {'upload_file': open("/home/myuser/blablabla/testfile.jpg)", "rb")}
params = {"metadata":json.dumps(metadata), "file": "filename"}
headers = {
"content-type": "application/json",
"Authorization": 'Bearer ' + token,
"accept-encoding": "gzip, deflate"
}
response = requests.post(url, headers = headers, params = params, files = fileForUpload)
I receive an error message of the API-endpoint, claiming that the file parameter is missing ...
Any idea what I am doing wrong?

Tomtom webfleet order api gives 40 general error

I am trying to insert a destination order using action=insertDestinationOrder
I am using POST method with all required parameters, but keep getting
{
"errorCode": 40,
"errorMsg": "general error"
}
I have checked using postman too. But still same.
Below is the request using python requests package.
import requests
url = "https://csv.telematics.tomtom.com/extern"
payload = "orderid=TO0049&country=DE&city=Cologne&latitude=50974519&ordertype=delivery%20order&zip=50735&longitude=6977319&street=Am%20Niehler%20Hafen%20%26%20Stapelkai%2C%2050735%20Cologne%20(Niehl)&account=XXXX&username=XXXX&password=XXXX&apikey=XXXX&lang=en&action=insertDestinationOrder&ordertext=Am%20Niehler%20Hafen%20%26%20Stapelkai%2C%2050735%20Cologne%20(Niehl)&useUTF8=true&outputformat=json"
headers = {
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
Need some help.
Try adding a ? to the end of the url, or to the start of the payload:
url = "https://csv.telematics.tomtom.com/extern?"
or
payload = "?orderid=TO0049&country=DE&city=Cologne&latitude=50974519&ordertype=delivery%20order&zip=50735&longitude=6977319&street=Am%20Niehler%20Hafen%20%26%20Stapelkai%2C%2050735%20Cologne%20(Niehl)&account=XXXX&username=XXXX&password=XXXX&apikey=XXXX&lang=en&action=insertDestinationOrder&ordertext=Am%20Niehler%20Hafen%20%26%20Stapelkai%2C%2050735%20Cologne%20(Niehl)&useUTF8=true&outputformat=json"

Python Requests Microsoft Graph API Authentication

I'm having issues receiving the bearer token using Python for the Microsoft Graph API. Here is what I have so far:
import requests
import json
headers = {
'Content-Type': 'x-www-form-urlencoded',
'Authorization': 'Basic'
}
data = {
"grant_type": "client_credentials",
"client_id" :"<client_id>",
"client_secret": "<client_secret>",
"resource": "https://graph.microsoft.com"
}
r = requests.post('<token_address>', headers=headers, data=data)
print(r.text)
I have it worked in Postman, through x-www-form-urlencoded, but can't seem to get it working in Python. It returns The request body must contain the following parameter: 'grant_type'. I realize the problem probably has to do with needed the data converted, but I am not sure where to start.
You're sending some invalid headers in your request:
The Content-Type should be application/x-www-form-urlencoded not x-www-form-urlencoded.
You shouldn't be sending an Authorization header at all.
Technically, since requests.post sends data as form encoded by default, you can safely remove your headers from the request:
payload = {
'grant_type': 'client_credentials',
'client_id': '<client_id>',
'client_secret': '<client_secret>',
'resource': 'https://graph.microsoft.com',
}
r = requests.post('https://login.microsoftonline.com/common/oauth2/token', data=payload)
print(r.text)
I believe that OAuth expects the body to be URL-encoded, like this:
data = "grant_type=client_credentials"
+ "&client_id=<client_id>"
+ "&client_secret=<client_secret>"
+ "&resource=https://graph.microsoft.com"

Exporting gooddata report python

Looking to connect to the Gooddata API and export a report via the API in python. The documentation is a bit confusing to follow.
I've defined a login to my instance of gooddata:
from urllib2 import Request, urlopen
import json
import requests
def login_gooddata(my_email, my_password):
url = 'https://secure.gooddata.com/gdc/account/login'
values = {
"postUserLogin": {
"login": my_email,
"password": my_password,
"remember": 0,
"verify_level": 0
}
}
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
encoded_values = json.dumps(values)
#request = Request(url, data=encoded_values, headers=headers)
r = requests.post(url, data=encoded_values)
return r
That successfully logs me in, returning a 200 response.
Given the documentation from the gooddata website on connecting to the API, I'm trying to export a raw project file.
I set the project and object ids:
project_id = 'asibfakuyebkbhdbfaisdf'
object_id = '87234760'
values = {
"report_req": {
"reportDefinition": "/gdc/md/"+ project_id + "/obj/" + object_id
}
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
url = 'https://secure.gooddata.com/gdc/app/projects/' + project_id + '/execute/raw/'
r = requests.post(url, data=json.dumps(values), headers=headers)
request = Request(url, data=json.dumps(values), headers=headers)
response_body = urlopen(requests).read()
print response_body
I played around with using r = requests.post(url, data=encoded_values and request = Request(url, data=encoded_values, headers=headers). Still receiving an error. I'm not really sure how to tackle next steps.
Following directions as stated in documentation for connecting to the API:
You need to perform all HTTP requests from a single "session" that remembers cookies from the login: perform s = requests.Session() once, then use s.post instead of requests.post.
See https://stackoverflow.com/a/31571805/3407728 for more.

Python POST request error "image format unsupported" using Microsoft Face API

I'm trying to send a binary image file to test the Microsoft Face API. Using POSTMAN works perfectly and I get back a faceId as expected. However, I try to transition that to Python code and it's currently giving me this error:
{"error": {"code": "InvalidImage", "message": "Decoding error, image format unsupported."}}
I read this SO post but it doesn't help. Here's my code for sending requests. I'm trying to mimic what POSTMAN is doing such as labeling it with the header application/octet-stream but it's not working. Any ideas?
url = "https://api.projectoxford.ai/face/v1.0/detect"
headers = {
'ocp-apim-subscription-key': "<key>",
'content-type': "application/octet-stream",
'cache-control': "no-cache",
}
data = open('IMG_0670.jpg', 'rb')
files = {'IMG_0670.jpg': ('IMG_0670.jpg', data, 'application/octet-stream')}
response = requests.post(url, headers=headers, files=files)
print(response.text)
So the API endpoint takes a byte array but also requires the input body param as data, not files. Anyway, this code below works for me.
url = "https://api.projectoxford.ai/face/v1.0/detect"
headers = {
'ocp-apim-subscription-key': "<key>",
'Content-Type': "application/octet-stream",
'cache-control': "no-cache",
}
data = open('IMG_0670.jpg', 'rb').read()
response = requests.post(url, headers=headers, data=data)
print(response.text)

Categories