Tomtom webfleet order api gives 40 general error - python

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"

Related

How to delete an attachable using the Quickbooks API?

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)

Python API Error - 'faultstring': 'JSONThreatProtection

First time trying to extract data via an API. I'm getting stuck with how to pass the raw data. I have been able to get it to work in postman but I can't quite figure it out with Python.
I've been getting this error:
{'fault': {'faultstring': 'JSONThreatProtection[JSON-Threat-Protection-1]: Execution failed. reason: Expecting { or [ at line 1', 'detail': {'errorcode': 'steps.jsonthreatprotection.ExecutionFailed'}}}
I'm pretty sure its the data portion that is wrong but I haven't been able to figure out if it's just a syntax error or something bigger that I'm missing.
My code as follows:
import requests
import json
url = "https://url/customers/shipmentstatus"
headers = {
"Authorization": "Bearer TOKEN",
"Content-Type": "application/json"
}
data = {
"Id": [
"AZ1234"
]
}
response = requests.post(url, headers=headers, data=data)
print(response.json())
Your code posts the data as form encoded data, not as JSON. That's because you use the data parameter to supply the payload. To fix, encode the data as JSON using json.dumps():
response = requests.post(url, headers=headers, data=json.dumps(data))
But a better way is to pass the data to requests using the json parameter:
response = requests.post(url, headers=headers, json=data)
Using the json parameter is easier and it will also add the Content-Type: application/json header for you.

I have some troubles with a post request

I'm trying to scrape a website but i get a 500 response.
It's a relocation website and i'm trying to get prices for different input parameters (volume, destination, departureCity ...).
import requests
from bs4 import BeautifulSoup
url = "https://www.demenagerfacile.com/devis/create"
payload = "volume_set_with_vcalc=false&id_vc_quote=&mode=MOVE_CLASSIC&fromCity=150+Rue+Saint-Maur%2C+Paris%2C+France&locationDeparture=%7B%22lat%22%3A%2248.8690238%22%2C%22lng%22%3A%222.3745715999999675%22%7D&placeIdDeparture=%22ChIJOTRebuRt5kcRoklIrmMZx6Y%22&countryDep=FR&toCity=Rue+de+Marseille%2C+Lyon%2C+France&locationDestination=%7B%22lat%22%3A%2245.7509119%22%2C%22lng%22%3A%224.83963289999997%22%7D&placeIdDestination=%22Eh5SdWUgZGUgTWFyc2VpbGxlLCBMeW9uLCBGcmFuY2UiLiosChQKEgm7NI_oRer0RxEcVzzVtXmrQxIUChIJl4foalHq9EcR8CG75CqrCAQ%22&countryDest=FR&fixedDate=on&date=2019-07-18&volume=30"
headers = {
"content-type": "application/x-www-form-urlencoded",
"cookie": "PHPSESSID=k3f99mg9urimu9dpc1ot7vudf5; __stripe_mid=e5c971f7-5e78-4e61-92db-a62dda48fd2c; _ga=GA1.2.110393239.1561101566; _gid=GA1.2.1597485519.1561101566; previousServerId=0; _fbp=fb.1.1561101566214.588023040; _hjIncludedInSample=1; hubspotutk=e7e9f524c880e1091d415cfc5cbd4903; __hssrc=1; apzContact=%5B%7B%22buttonIdKey%22%3A%229deacb07173c79d76baf4f58c56037ad%22%2C%22apizeeSessionId%22%3A%22334d6232-930f-41ec-aab9-0753c7b3036a%22%2C%22apiconfSession%22%3A%229deacb07173c79d76baf4f58c56037ad%22%2C%22apiconfOfflineMessageState%22%3Anull%7D%5D; cookiebanner-accepted=1; __stripe_sid=5e2fb6df-ea5c-470a-98f3-44e172955633; __hstc=78993675.e7e9f524c880e1091d415cfc5cbd4903.1561101566944.1561101566944.1561106755827.2; _gat_UA-65262710-1=1; __hssc=78993675.3.1561106755827; apiCCId=334d6232-930f-41ec-aab9-0753c7b3036a-0000; apiKey=d7897a4712a9413c3a363e737954d853; sessionId=230a5a70-9401-11e9-935a-19ca5a0a60c1",
}
response = requests.request("POST", url, data=payload)
print(response)
Requests’ simple API means that all forms of HTTP request are as obvious. For example, this is how you make an HTTP POST request
Replace your code
response = requests.request("POST", url, data=payload)
TO
response = requests.post(url,headers=headers, data=payload)
Hi #mhabak1 I just checked and it seems to be working,
Please add headers while posting the request like below,
response = requests.post(url, data=payload, headers=headers)
Thanks

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"

Add a member using python requests and Mailchimp API v3

I am trying to learn more about using http requests and the Mailchimp API, but I cannot seem to figure out how to add a member to list using a post request. I have tried multiple configurations, and I guess I made some headway since my response went from a 405, to a 401, now I'm getting a 400. I assume this means that I am being authenticated, but I am formatting the request incorrectly.
I have gotten it to work the python mailchimp library, but I want to actually learn how to use the HTTP requests. I could find very few examples of using python requests with Mailchimp.
(obviously I put in my actual list_id, my_username, and my_apikey)
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'blah#blah.com',
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
payload = json.dumps(data1)
response = requests.post(members_url, auth=auth, headers=headers, json=payload)
This is my response:
>>> response
<Response [400]>
I'm stumped....what am I doing wrong?
This worked:
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'<a real email address>', #it could tell 'blah#blah.com was fake'
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
response = requests.post(members_url, auth=auth, headers=headers, json=data1)
This worked:
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'<a real email address>', #it could tell 'blah#blah.com was fake'
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
response = requests.post(members_url, auth=auth, headers=headers, json=data1)
My problems were #1 I was double encoding the json data and #2 mailchimp recognizes obviously BS email addresses like blah#blah.com
Thanks #Alasdair

Categories