Hi
I hope you can help.
I am trying to connect to an API and have been given the following information. I do not have much experience in this area and any help would be appreciated.
Authentication
Purpose - Authorization fro subsequent calls
method - POST https only
URL - {baseURL}/Token
request - format x-www-form-urlencoded
data
username - as provided
password - as provided
Grant_Type - must be 'password'
response
format JSON
data access_token - token will be need for subsequent calls
So i used the following code - which is taken from an example on this forum and I get a response of -1 - any help in debugging would be usefull also
I am using pb2019 build 2170 - i am happy to upgrade to 2019r2 or 2019r3 if that will help
************************
string ls_url = 'https://apiplay.cleanservices.co.uk/token/'
string ls_clientid = 'lm.computers'
string ls_clientsecret = 'secret' // password not shown
string ls_scope = ''
string ls_auth = ''
string ls_base64 = ''
string ls_body = ''
integer li_rc
string ls_token
RestClient lo_restclient
CoderObject lo_coderobject
lo_coderobject = create CoderObject
lo_restclient = create RestClient
ls_auth = ls_clientid + ':' + ls_clientsecret
ls_base64 = lo_coderobject.Base64Encode(Blob(ls_auth, EncodingUTF8!))
// Set the authorization and content headers
lo_restclient.SetRequestHeader('Authorization', 'Bearer ' + ls_base64)
lo_restclient.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
// Using the GetOauthToken function
// Create the token request
TokenRequest lo_tokenrequest
lo_tokenrequest.TokenLocation = ls_url
lo_tokenrequest.Method = 'POST'
lo_tokenrequest.GrantType = 'password'
lo_tokenrequest.ClientId = ''
lo_tokenrequest.ClientSecret = ''
lo_tokenrequest.UserName = ls_clientid
lo_tokenrequest.PassWord = ls_clientsecret
lo_tokenrequest.Scope = ls_scope
// Get token with GetOauthtoken (this returns the token as a string already parsed from the json response)
li_rc = lo_restclient.GetOauthtoken(lo_tokenrequest, ls_Token)
destroy lo_coderobject
destroy lo_restclient
return
Yes i realised eventually I needed Basic, I had that originally and changed it clutching at straws !
missing Basic was first mistake.
Regarding the other changes, I have still doubts on what made the code working because:
- you create the body but don't use it;
- you set Authorization request header which, according to PB doc, causes ClientId and ClientSecret properties to be ignored.
Cheers,
.m
Agree I set body and dont use it that was left over code from when i tried to use JWTtoken - that worked - i picked up the token but i couldnt then use it. so that code should be removed - this was my JWT code
// Get the token with GetJWTToken (this returns the full json of the response which needs to be parsed to get the token)
li_rc = lo_restclient.GetJWTToken(ls_url, ls_body, ls_token)
I agree about what it says re clientid and secret - however it did not work until i added those - it was a guess to be honest.
I later set the token and post using the following code and have confirmation from the people managing the server data that my records get sent.
ls_json_data='[ { "RFIDTagId": "00007D310A01131B13F160EC", "ItemReference": "700.001 WHIT", "LastScanTime": "2021-04-14T10:01:23", "LastScanType": "Dirty", "LastScanLocation": "Hotel", "IsKilled": "false" }, { "RFIDTagId": "00007D310A01131B13F140EC", "ItemReference ": "700.001 WHIT", "LastScanTime": "2021-04-14T10:02:13", "LastScanType": "Dirty", "LastScanLocation": "Hotel", "IsKilled": "false" } ]'
if li_rc=1 then
lo_restclient.SetOauthToken(ls_token2)
li_post_return=lo_restclient.SendPostRequest("https://apiplay.cleanservices.co.uk/SupplierIntegration/RFIDMovementEntry", ls_json_data, ls_post_responsebody)
li_post_return_code=lo_restclient.GetResponseStatusCode()
ls_post_return_text=lo_restclient.GetResponseStatusText()
end if