Add new contact in Xero using python FASTAPI - python

I am trying to add a new contact in Xero using python FASTAPI:
define a new contact
contact = {
"Contacts": [
{
"Name": "24 locks",
"FirstName": "Ben",
"LastName": "Bowden",
"EmailAddress": "ben.bowden#24locks.com",
"ContactPersons": [
{
"FirstName": "John",
"LastName": "Smith",
"EmailAddress": "john.smith#24locks.com",
"IncludeInEmails": "true"
}
]
}
]
}
Call xero API
get_url = 'https://api.xero.com/api.xro/2.0/Contacts'
response = requests.post(get_url,
headers = {
'Authorization': 'Bearer ' + access_token,
'Xero-tenant-id': xero_tenant_id,
'Accept': 'application/json'
},
data = contact
)
json_response = response.json()
I get the following error:
{'ErrorNumber': 17, 'Type': 'NoDataProcessedException', 'Message': 'No data has been processed for this endpoint. This endpoint is expecting Contact data to be specifed in the request body.'}
Someone can help please ?
You can assume that access_token and xero_tenant_id are correct as I am using them for other methods and they works fine.
Thanks

Try to use json = contact instead of data = contact or set header 'Content-Type': 'application/json'
Because
data for dict When not specified content-type, The default is application/x-www-form-urlencoded,

Related

Microsoft Graph API how to send an attachment in a chat

I want to use the Microsoft Graph API to send messages with attachments to chats or channels.
https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=http#example-4-send-a-message-with-file-attachment-in-it
I can send normal messages already just fine like this:
def post_message(chat_id: str, subject: str = "", content_type: str = "text", content: str = "") -> None:
url = f"https://graph.microsoft.com/v1.0/chats/{chat_id}/messages"
json = {
"subject": subject,
"body": {
"contentType": content_type,
"content": content
}
}
res = requests.post(url, headers=header, json=json)
I try to copy the body from the example in the link above, substitute for my values and swap json variable for this one:
attachment_id = '7QW90B10D7-B5AK-420A-AC78-1156324A54F2' # not real, only to show how it looks like
json = {
"body": {
"contentType": 'html',
"content": f'i dunno what i\'m doing. <attachment id="{attachment_id}"></attachment>'
},
'attachments': [
{
'id': attachment_id,
'contentType': 'reference',
'contentUrl': 'https://foo.sharepoint.com/sites/bar/User%20documentation/Databricks/Databricks%20guide.pptx',
'name': 'Databricks guide.pptx'
}
]
}
I get requests.exceptions.HTTPError: 400 Client Error: Bad Request for url
What's wrong with the code? How to get attachment id from a file correctly because I am not sure I got the right value?
I was able to get it working with your code, (using both formats <attachment id=\"\"> and <attachment id="">), so it seems the error is probably with your attachment_id.
I retrieved the driveItem ID by following this answer, where the driveItem ID is the GUID value in the eTag property in the response.
You could get the file by the path:
https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/{item-path}
For example:
https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/test.docx
If the file is in a folder, it would be like this:
https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/folder1/test.docx
Sample request after obtaining the ID
access_token = ""
attachment_name = "file.txt"
attachment_path = "https://domain.sharepoint.com/Shared%20Documents"
attachment_id = "12345678-1234-1234-1234-123456789123"
attachment_url = f"{attachment_path}/{attachment_name}"
chat_id = ""
req_url = f"https://graph.microsoft.com/v1.0/chats/{chat_id}/messages"
req_headers = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json"
}
json = {
"body": {
"contentType": "html",
"content": f"Test message <attachment id=\"{attachment_id}\"></attachment>"
},
"attachments": [
{
"id": attachment_id,
"contentType": "reference",
"contentUrl": attachment_url,
"name": attachment_name
}
]
}
result = requests.post(url = req_url, headers = req_headers, json = json)

TF400898: An Internal Error Occurred. Activity Id: 1fc05eca-fed8-4065-ae1a-fc8f2741c0ea

i’m trying to push files into git repo via azure API but getting activity_id error. I followed their documentation and trying to add simple file in my repo.
Here is my code:
import requests, base64
pat_token = "xxxx-xxxxx-xxxx"
b64Val = base64.b64encode(pat_token.encode()).decode()
payload = {
"refUpdates": [
{
"name": "refs/heads/main",
"oldObjectId": "505aae1f15ae153b7fc53e8bdb79ac997caa99e6"
}
],
"commits": [
{
"comment": "Added task markdown file.",
"changes": [
{
"changeType": "add",
"item": {
"path": "TimeStamps.txt"
},
"newContent": {
"content": "# Tasks\n\n* Item 1\n* Item 2",
"contentType": "rawtext"
}
}
]
}
]
}
headers = {
'Authorization': 'Basic %s' % b64Val,
'Content-Type': 'application/json',
}
params = (
('api-version', '6.0'),
)
response = requests.post('https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repo}/pushes', headers=headers, data=payload, params=params)
Anyone knows how to solve this issue? I have also added this issue on their developer community
I’ve fixed that error, actually the payload was not in json format so i have to make it as json and after that it worked fine.
Like this
response = requests.post('https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repoId}/pushes', headers=headers, params=params, data=json.dumps(payload))

Python Post Request to Custom API

Apologies if this is a stupid problem, but I do not have crazy amounts of experience with API's.
I have a request to do a REST API Call to post a JSON file to a endpoint via a Python script.
This is done in 2 steps:
Generate OAuth token from details received (grant_type, client_id, client_secret, scope)
Use this token to post the JSON structure file into the endpoint
However, I am having troubles posting the JSON file in my request.post call (I have edited the code to not show the sensitive information):
url_token = "https://login.dummyurl/oauth2/v2.0/token"
payload = {
'grant_type': 'client_credentials',
'client_id' : '<Dummy_ID>',
'client_secret' : '<Dummy_Secret>',
'scope' : 'https:<dummyscope>.default'
}
headers = {'accept': "application/json", 'content-type': "application/x-www-form-urlencoded"}
response = requests.post(url_token, data=payload, headers=headers)
return_obj = json.loads(response.text)
This generates the token that I need, but the problem is in the following section.
I read the JSON file as follow:
f = open(file, 'r')
cust_data = json.load(f)
Then create my "header" with the auth token included and set my target URL:
hed = {'accept': 'application/json', 'Authorization': token_type + ' ' + access_token}
url_cust = 'https://api-eu.jdadelivers.com/<dummy>/<dummyChannel>/v1/<pull_data>/<endpoint>'
Then I run the POST request to post the data to the endpoint:
response_post = requests.post(url_cust, data=json.dumps(cust_data), headers =hed)
print(response_post.json())
But I am prompted with the following error:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I have researched all the different methods to pull in the file differently, as it indicates that there might be a issue with the quotes etc. But I have attached a dummy sample of the JSON file I am trying to ingest.
{
"header": {
"sender": "SENDER",
"receiver": "RECEUVER",
"model": "MODEL",
"messageVersion": "VERSION",
"messageId": "DUMMY_ID",
"type": "ENDPOINT_DUMMY",
"creationDateAndTime": "2020-07-20T11:30:49.115+05:30"
},
"demandChannel": [
{
"creationDateTime": "2020-07-20T11:30:49.579+05:30",
"documentStatusCode": "STATUS",
"documentActionCode": "CODE",
"demandChannelId": "1",
"description": "DUMMY_TEST",
"componentDelimiter": "-",
"avpList": [
{
"CUSTOMERLVL_3": "TEST",
"CUSTOMERLVL_3_DESCR": "",
"CUSTOMERLVL_2": "TEST",
"CUSTOMERLVL_2_DESCR": "",
"CUSTOMERLVL_1": "TEST",
"ATTRIBUTE_1": "01",
"ATTRIBUTE_2": "TEST",
"ATTRIBUTE_3": "TEST",
"ATTRIBUTE_4": "TEST",
"ATTRIBUTE_5": "TEST",
"ATTRIBUTE_6": "TEST",
"ATTRIBUTE_7": "TEST",
"ATTRIBUTE_8": "TEST",
"ATTRIBUTE_9": "TEST",
"ATTRIBUTE_10": "TEST",
"ATTRIBUTE_11": "",
"ATTRIBUTE_12": "",
"ATTRIBUTE_13": "",
"ATTRIBUTE_14": "X",
"ATTRIBUTE_15": ""
}
]
}
]
}
Your help is greatly appreciated.
I found the problem - because of the custom API, I had to state the in the header dictionary the "User-Agent" parameter, and then posted successfully.

openstack cannot retrieve authentication token from API

I am trying to retrieve the authentication token from the API using requests library from python. Here is the attempt I have made so far:
def get_token():
data = {
"auth" : {
"identity" : {
"methods" : [ "password" ],
"password": {
"user" : {
"name" : OS_USERNAME,
"domain": { "name": "Default" },
"password": OS_PASSWORD
}
}
}
}
}
r = requests.post(
OS_AUTH_URL+'/auth/tokens',
headers = HEADERS,
json = data, # https://stackoverflow.com/questions/9733638
verify = False
)
print(r.content)
j = json.loads(r.content)
return j['token']['user']['id']
I get token in the response :
{
"token": {
"issued_at": "2018-07-03T11:03:59.000000Z",
"audit_ids": [
"Fg1ywtZBQ1CkigCw70If9g"
],
"methods": [
"password"
],
"expires_at": "2018-07-03T12:03:59.000000Z",
"user": {
"password_expires_at": null,
"domain": {
"id": "default",
"name": "Default"
},
"id": "e0dc5beb383a46b98dad824c5d76e719",
"name": "admin"
}
}
}
However, when I am reusing this token to get, for instance, the list of projects :
def get_tenantID():
r = requests.get(
OS_AUTH_URL+'/auth/projects',
headers = HEADERS,
verify = False
)
return r
r = get_token()
HEADERS['X-Auth-Project-Id'] = 'admin'
HEADERS['X-Auth-Token'] = r
r = get_tenantID()
I get this error as if I would not be authenticated:
<Response [401]>
{"error": {"message": "The request you have made requires authentication.", "code": 401, "title": "Unauthorized"}}
Googling around and using openstack token issue command showed me that usually, token are more like:
gAAAAABaCo1F1CIMVlsTBeuqYH8tm2qR29tbkmUL4vZuhCNPXJI39TQ2YzL6Twoj8fNcAyLe3WhCYW2O1YpbBF0G8mo4bt7Kf0IRsoDOoJ6uWa3RYyJ5SQNoB_5n8EnVXbKPxFYOZ_iFBnaVtL1_XDrGbwsrlDeyy8lZTDdLsqY52pUhFR-7Uow
which is not what I get with get_token.
What am I doing wrong?
Many thanks!
When you use the auth API directly, the token issued comes in the X-Subject-Token header.
Thus, to retrieve in your python example, you could access the response.headers dict like this:
token = r.headers['X-Subject-Token']
More info about authentication in the Keystone v3 docs
1. fetch authentication token as mentioned below:
r = requests.post(
OS_AUTH_URL+'/auth/tokens',
headers = HEADERS,
json = data,
verify = False
)
token = r.headers[X-Subject-Token]
2. Pass this token in header for further request:
{
'X-Auth-Token': token
}

Python post api request

I need to make a post into an API. I'm working with python. I'm new on this and I can't create an ad tag. I tried with create a dict with the api example information but it didn't work. When I run the >>> sitios_creados, the answer is ''
and when I run sites.status_codeI reveice `415.
I don't understand why because if you see in my code, I did a right post python requests before with the token
I must to take the publisherid and with it and the token id create the ad tag.
my publisherid is: 15663
my code:
import requests
import json
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ',
}
data = [
('grant_type', 'password'),
('username', ''),
('password', ''),
]
response = requests.post('http://api.site.com/v1/oauth/generateOauthToken', headers=headers, data=data)
json_data = json.loads(response.text)
token = json_data['access_token'].encode("utf-8")
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer {}'.format(token)
}
sites = requests.post('http://api.site.com/v1/inventorymgmt/publisherAdTag?entityId=15663', headers=headers, data=data)
sitios_creados = sites.content
Api information example:
URL: http://api.site.com/v1/inventorymgmt/publisherAdTag?entityId=2685
Method: POST
Request Body:
{
"publisherId": 2685,
"publisherSiteurl": "http://example.org",
"adTagName": "THIS_IS_TEST_DEMAND_5",
"adCodeTypeId": 1,
"foldPlacementId": 1,
"adTypeId": 3,
"pagePlacementId": 1,
"adExpansionDirectionId": 1,
"adSize": {
"name": null,
"width": 0,
"height": 0,
"id": 9
},
"adTagPlacements": [{
"adTagPlacementId": 0,
"linkOnlyToGeo": false,
"ecpm": 1,
"adScript": "THIS IS DEMO SCRIPT",
"currency": 1
}],
"adTagCustomParamMap": [{
"name": "kadcarrier",
"macroValue": "techno.carrier"
}, {
"name": "kadcity",
"macroValue": "geo.city"
}]
}
Is it an API for a website?
When yes, you can do a network analysis in the developer tool of your browser and copy the curl command of the POST package.
Then you surf to curl.trillworks.com and convert the curl command it into a Python POST request.
Inside of your python request you can modify the values.

Categories