1. Andrew Davis
  2. PowerBuilder
  3. Wednesday, 14 April 2021 10:42 AM UTC

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

 

Marco Meoni Accepted Answer Pending Moderation
  1. Wednesday, 14 April 2021 17:40 PM UTC
  2. PowerBuilder
  3. # 1

Hi Andrew,

looks like in the header you are sending Basic auth (usr:pwd), not a Bearer token.

Also, you specify content-type form encoded, where is it?

I advise you try first from Postman/SoapUI and when working reproduce in PB ode.

Cheers,

.m

 

Comment
  1. Andrew Davis
  2. Wednesday, 14 April 2021 17:47 PM UTC
Thanks

Yes i realised eventually I needed Basic, I had that originally and changed it clutching at straws !

  1. Helpful
  1. Marco Meoni
  2. Wednesday, 14 April 2021 17:56 PM UTC
yes,

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

  1. Helpful
  1. Andrew Davis
  2. Thursday, 15 April 2021 10:14 AM UTC
Marco



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



  1. Helpful
There are no comments made yet.
Andrew Davis Accepted Answer Pending Moderation
  1. Wednesday, 14 April 2021 17:43 PM UTC
  2. PowerBuilder
  3. # 2

Just thought i would let you know I have now got it working. I did install pb2019r3 on a machine, the working code is below in case anyone is interested.

 

string ls_json_data,ls_post_responsebody,ls_client64,ls_clientsecret64,ls_post_return_text
integer li_post_return,li_post_return_code

string ls_url = 'https://apiplay.cleanservices.co.uk/token'
string ls_clientid = 'username=lm.computers' // Add your real client id here
string ls_clientsecret = 'password=password.....' // Add your real client secret here
string ls_username='lm.computers'
string ls_password='password.....'
string ls_scope = ''
string ls_auth = ''
string ls_base64 = ''
string ls_body = ''
integer li_rc
string ls_token,ls_token2

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!))
ls_client64= lo_coderobject.Base64Encode(Blob(ls_clientid, EncodingUTF8!))
ls_clientsecret64=lo_coderobject.Base64Encode(Blob(ls_clientsecret, EncodingUTF8!))

// Set the authorization and content headers
lo_restclient.SetRequestHeader('Authorization', 'Basic ' + ls_base64)
lo_restclient.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

// Create the body
ls_body = 'grant_type=password'
ls_body += '&username=' + ls_username
ls_body += '&password=' + ls_password
//ls_body += '&scope=' + ls_scope

// 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 = ls_client64
lo_tokenrequest.ClientSecret = ls_clientsecret64
lo_tokenrequest.UserName = ls_username
lo_tokenrequest.PassWord = ls_password
//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_Token2)

Comment
  1. Armeen Mazda @Appeon
  2. Wednesday, 14 April 2021 17:47 PM UTC
Thanks for sharing the solution!
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.