Get API token key to be able to post orders

160 views
Skip to first unread message

David Friday

unread,
May 26, 2021, 8:31:12 PM5/26/21
to Bots Open Source EDI Translator
Has anybody been able to post to an API using a username/password and the response is a token key which is needed to post a json order file that is generated from a BOTS translation.

Example: 
The response will be a {"access_token": "lajfdi0fe089r"} which is used to post json order file to http://domain.com/Api/POSTSubmitOrders

I'm currently able to post a json order file using a predefined key shown below from BOTS but not sure if what I described above is possible in the same communicationscript.
'uri': '/sites/api/order/post',
'mode': 'https',
'type': 'body',
'header': {
'Authorization': '7ddd552-1h34-4fac-92d1-1622225w46b', 'Content-Type': 'application/json'

If anybody has done this I would appreciate your help or advice on how to make it work.

Thanks,

David

Eppye Bots

unread,
May 27, 2021, 4:57:18 AM5/27/21
to 'Chuck Turco' via Bots Open Source EDI Translator
yes, it can be done.

you will need scripting.
key has to be stored (in database, or on file system)

if no active key:
    get key()

do_call()
if key not valid:
   get key
   do call again()




kind regards, Henk-Jan Ebbers


--
You received this message because you are subscribed to the Google Groups "Bots Open Source EDI Translator" group.
To unsubscribe from this group and stop receiving emails from it, send an email to botsmail+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/botsmail/ab13ff25-3f2b-4868-905c-6dee45ae9a2dn%40googlegroups.com.
Message has been deleted

David Friday

unread,
May 28, 2021, 4:47:14 PM5/28/21
to Bots Open Source EDI Translator
Thank you for your response Henk-Jan,

Please correct me if below is not possible:

 Using only one communication script,  within connect(channeldict) I place the URL and in body I will place grant_type=password&username=XXX&password=ABC1234  which will be sent to the API server.
 The response will be the access_token (changes every hour) that I can store as a global variable which is passed down to main as I have shown below:

def main(channeldict,filename,*args,**kargs):
    if os.path.exists(filename):
paramsDict = {
'uri': '/Api/POST-Order-SubmitOrders',
'mode': 'https',
'type': 'body',
'header': {
'Authorization': access_token, 'Content-Type': 'application/json'
},
}

If this will not work in one communication script then is there an example where I can pull a communication script into this script.

Thanks for any help,

David

Eppye Bots

unread,
May 30, 2021, 10:20:44 AM5/30/21
to 'Chuck Turco' via Bots Open Source EDI Translator
?? I do not know what that code is.

best is (for communicationscript to sub-class existing communication class.
(sould be some exampels of sub-classing in mailing list)

kind regards, Henk-Jan Ebbers


David Friday

unread,
Jun 4, 2021, 9:34:14 AM6/4/21
to Bots Open Source EDI Translator
Hi Henk-Jan,

I'm able to connect using the below communication script, but unable to receive and store the response which is the access_token. Have you had a chance to look at what I sent via your business email?

def connect(channeldict):
    print 'connect',channeldict['idchannel']

def main(channeldict,*args,**kargs):
paramsDict = {
'mode': 'https',
'uri': '/Axis/Login',
'header': {
'Content-Type': 'application/x-www-form-urlencoded'
},
}
    
url = paramsDict.get('mode') + "://" + channeldict['host'] + paramsDict.get('uri') # Adds param dictionary items to make up URL
    
outResponse = requests.post(url, headers = paramsDict.get('header'), data = 'grant_type=password&username=GEDI&password=xxx96!1')
if (outResponse.status_code != requests.codes.ok):
raise Exception(_(u'Http send error, response code is %s and content is %s.')%(outResponse.status_code, outResponse.text))

response = (outResponse.content)
if not response['status'] or response['message']:
raise botslib.CommunicationError('Status: %(status)s, Message: %(message)s' %response)

def disconnect(channeldict):
    print 'disconnect',channeldict['idchannel']

Thanks,

David

Eppye Bots

unread,
Jun 4, 2021, 10:04:09 AM6/4/21
to 'Chuck Turco' via Bots Open Source EDI Translator
no, I have not had that time.
it it clear where the token should be gotten from?


kind regards, Henk-Jan Ebbers


Hans

unread,
Jul 8, 2021, 8:28:22 AM7/8/21
to Bots Open Source EDI Translator
Hi David, you could extract the token out of the the JSON and then use it to post your order.

#POST request for token code
URLremotelogin = self.channeldict['host']
data = {'user': self.channeldict['username'], 'password': self.channeldict['secret']}
outResponse = self.session.post(URLremotelogin, data=data)

#Extract Token code from response
Response = json.loads(outResponse.content)
token = Response['Token']

For more info regaring the communication script to send api call see https://github.com/bots-edi/bots/issues/51
Op vrijdag 4 juni 2021 om 16:04:09 UTC+2 schreef eppye:

David Friday

unread,
Jul 8, 2021, 9:42:29 AM7/8/21
to Bots Open Source EDI Translator
Hi Hans,

Yes, my setup firstly does a login via API and downloads the token to a file named access_token, then I used a routescript to remove data not needed in the access_token file and finally posted a translated json order file to the API using the access_token file as my authentication.

Hope this helps,

David

David Friday

unread,
Jul 8, 2021, 9:53:51 AM7/8/21
to Bots Open Source EDI Translator
Forgot to add that I had added the access_token to the persist table and called it during my post.

    token = transform.persist_lookup(domein, botskey)
#    print token
    if os.path.exists(filename):
paramsDict = {
'uri': '/SubmitOrders',
'mode': 'https',
'type': 'body',
'header': {
'authorization': 'Bearer ' + token, 'Content-Type': 'application/json'
},
}


Hans

unread,
Jul 8, 2021, 11:56:07 AM7/8/21
to Bots Open Source EDI Translator
Hi David,

In the example i've send, you can do all this in 1 route / communicationscript. so no need to have sevaral routes with files and persist actions.
I've added a communicationscript wich comes more in the direction you're asking.
i don't really know how you've scripted you communicationscript now, so i can't help you much with your setup.

Op donderdag 8 juli 2021 om 15:53:51 UTC+2 schreef David Friday:
ORDER2API+TOKEN.py

David Friday

unread,
Jul 8, 2021, 1:19:44 PM7/8/21
to Bots Open Source EDI Translator
Hi Hans,

Thank you very much I will test today and apply once completed.

This will cut down on system resources used to complete this translation.

Thanks again,

David

Reply all
Reply to author
Forward
0 new messages