Just a fun little piece I know I’ll want to look up later, in case it’s useful. I’m supporting ETQ in getting data from our system. They use Python scripts so I’m using that to prove anything I claim is working. In reality it seems they don’t support token auth, but that’s how I started out.
import base64
import requests
nl = "\n"
baseUrl = "https://[fqdnWithProperSSL.com]/[appServerWithBasicAuth]/"
apiKey = "anAPIKeyYouGenerated"
uName = "userNameWithAccesScope"
pwd = "YourPassword"
string_to_encode = uName + ":" + pwd
creds = base64.b64encode(string_to_encode.encode('utf-8')).decode('utf-8')
def doToken():
try:
svcUrl = baseUrl + "TokenResource.svc"
headers = {
"Authorization": "Basic " + creds
}
response = requests.post(svcUrl, headers=headers)
data = response.json()
for i in data:
if i == "AccessToken":
brTkn = str(data[i])
except Exception as e:
print("Tkn Err " + str(e))
finally:
return brTkn
def getVendGrp():
try:
tkn = doToken()
res = ""
headers = {
"Authorization": "Bearer " + tkn,
"x-api-key": apiKey
}
svcUrl = baseUrl + "api/v2/odata/[Company] /Erp.BO.VendGrupSvc/VendGrups"
response = requests.get(svcUrl,headers=headers)
data = response.json()
for i in data:
if str(i) == "value":
thing = data[i]
for d in thing:
res += d["GroupCode"] + " : " + d["GroupDesc"] + nl
except Exception as e:
print("VGrup Error " + str(e))
finally:
return res
print(getVendGrp())
This prints out the contents of our vendor groups table, needed because the Vendor object only contains the GroupCode and we have to look it up.
Py3 on my phone, just in case anyone mistakes me for someone with a life