- Andrew Davis
- PowerBuilder
- 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
Find Questions by Tag
Helpful?
If a reply or comment is helpful for you, please don’t hesitate to click the Helpful button. This action is further confirmation of their invaluable contribution to the Appeon Community.