Here's my python code:
import msal
import logging
import json
import requests
customer_id = 'removed'
def acquire_token():
config = json.load(open("parameters.json"))
app = msal.ConfidentialClientApplication(
config["client_id"], authority=config["authority"],
client_credential=config["secret"],
)
result = None
result = app.acquire_token_silent(config["scope"], account=None)
if not result:
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
result = app.acquire_token_for_client(scopes=config["scope"])
return result['access_token']
class Customer:
json = ""
def __init__(self, json):
self.json = json
print(self.json)
@staticmethod
def get_customers():
access_token = acquire_token()
url = f"api.businesscentral.dynamics.com/.../customers"
result = requests.get(
url,
headers={'Authorization': 'Bearer ' + access_token}, )
print("gd: %s" % result)
result_json = result.json()
print("Graph API call result: ")
print(json.dumps(result_json, indent=2))
@staticmethod
def query_customer(customer_number):
access_token = acquire_token()
url = f"api.businesscentral.dynamics.com/.../customers eq '{customer_number}'"
result = requests.get(
url,
headers={'Authorization': 'Bearer ' + access_token}, )
print("status: %s" % result.status_code)
result_json = result.json()
print("result data: ")
print(json.dumps(result_json, indent=2))
@classmethod
def create_customer(cls):
print(cls.json)
access_token = acquire_token()
url = f"api.businesscentral.dynamics.com/.../customers"
result = requests.post(
url,
data=cls.json,
headers={'Authorization': 'Bearer ' + access_token, 'Content-type': 'application/json',
'Content-Length': str(len(cls.json))}, )
print("status: %s" % result.status_code)
print("messsage: %s" % result.text)
Customer.get_customers()
Customer.query_customer("WHATEVAH")
customer = Customer(open('test_customer.json').read())
customer.create_customer()
And the current contents of my data file:
{
"number": "TEST1000",
"displayName": "Test Customer",
"type": "Company",
"addressLine1": "",
"addressLine2": "",
"city": "",
"state": "",
"country": "",
"postalCode": "",
"phoneNumber": "",
"email": "",
"website": "",
"salespersonCode": "",
"creditLimit": 0,
"taxLiable": false,
"taxRegistrationNumber": "",
"currencyId": "00000000-0000-0000-0000-000000000000",
"currencyCode": "USD",
"shipmentMethodId": "00000000-0000-0000-0000-000000000000",
"paymentMethodId": "00000000-0000-0000-0000-000000000000"
}