1. Simone Olianti
  2. PowerBuilder
  3. Thursday, 7 June 2018 21:18 PM UTC

hi there, i need to create a rest call like the example below:

POST /auth/realms/edoc/protocol/openid-connect/token
Authorization: Basic abcdefghilmnopqrstuvz
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=mario&password=XJp45pzH

i'm not sure if it's possible and how to set an authorization header HTTP Basic as requested using a client_id and a client_secret that i own

Here is part of the code i'm using which is missing the auth basic header as requested

httpclient http  
http = create httpclient 

ls_URL = ls_token_uri + '/auth/realms/edoc/protocol/openid-connect/token'
ls_mime = 'application/x-www-form-urlencoded'
ls_parm = 'grant_type=password&username=' + ls_username + '&password=' + ls_password
ls_url = ls_url + '?' + ls_parm

http.SetRequestHeader("Content-Type", ls_mime)
li_ret = http.sendrequest( 'POST', ls_url, '')  
if li_ret = 1 then
    li_rc = http.GetResponseStatusCode()
    if li_rc = 200 then http.GetResponseBody(ls_string)

    ...

any help would be appreciated
tia

Accepted Answer
Simone Olianti Accepted Answer Pending Moderation
  1. Friday, 8 June 2018 06:37 AM UTC
  2. PowerBuilder
  3. # Permalink

i've finally managed how to handle this. Here is the solution in case someone else having same question

basically to set the http basic authorization i had to crypt the client_id and client_secret in base64 (i.e "client_id:client_secret")
then set a header like this

ls_header = 'Basic ' + ls_encoded
http.SetRequestHeader("Authorization", ls_header)

everything working fine

p.s. also i had to pass the parameters of the call in the body

 

Comment
  1. Andrew Davis
  2. Friday, 8 June 2018 10:16 AM UTC
2 things hear - can you explain how you converted to base64 



and also how do you pass the parameters of the call in the body

  1. Helpful
  1. Simone Olianti
  2. Friday, 8 June 2018 11:27 AM UTC
Hi Andrew

to pass the parameters in the body:



ls_parm = ls_response_type + '&' + ls_user + '&' + ls_pass

bdata = blob(ls_parm, EncodingUTF8!)

li_ret = ln_http.sendrequest( 'POST', ls_url, bdata) 



to encrypt any string in base64 you can look at this really useful Bcrypt object developed by Roland Smith

http://www.topwizprogramming.com/freecode_bcrypt.html 

 

  1. Helpful
There are no comments made yet.


There are replies in this question but you are not allowed to view the replies from this question.