1. Andrew Davis
  2. PowerBuilder
  3. Friday, 8 June 2018 09:02 AM UTC

Hi

I hope someone can help, I have been reading the example on new REST features with PB2017 R2 - the article states it is using a a service that doesnt use authentication.

How would i use a service that requires authentication

The one i am using needs for example ( where asdf1234 is the password )

X-Auth BEARER asdf1234

regards

 

Andrew

René Ullrich Accepted Answer Pending Moderation
  1. Friday, 8 June 2018 10:38 AM UTC
  2. PowerBuilder
  3. # 1

The "Authorization" request header for basic authentication is a string that starts with "Basic ". Then follows a BASE64 codes string of "username:password".

For Base64 encoding you may use the CryptBinaryToStringW function of Windows API:

Function Boolean CryptBinaryToString ( &
    blob pbBinary, &
    ulong cbBinary, &
    ulong dwFlags, &
    Ref string pszString, &
    Ref ulong pcchString) Library "Crypt32.dll" Alias For "CryptBinaryToStringW"

 

blob lblob_text
string ls_b64
unsignedlong lul_buflen
CONSTANT ulong        CRYPT_STRING_BASE64     = 1
CONSTANT ulong        CRYPT_STRING_NOCRLF     = 1073741824


IF IsNull (as_text) THEN return as_text

lblob_text = Blob (as_text, EncodingUTF8!)
ls_b64 = Space (2 + 1.5 * (Len (lblob_text) + 2))
lul_buflen = Len (ls_b64)

IF Not CryptBinaryToString (lblob_text, Len (lblob_text), CRYPT_STRING_BASE64 + CRYPT_STRING_NOCRLF, ls_b64, lul_buflen) THEN
    // error handling
End If

return ls_b64

Comment
  1. Chris Pollach @Appeon
  2. Friday, 8 June 2018 14:12 PM UTC
Hi René et Al;



  FYI ... The next Appeon PB release PB2017R3 coming out next month - BASE64, Cryptography, etc methods will be built-in.  



Regards ... Chris

  1. Helpful
There are no comments made yet.
Simone Olianti Accepted Answer Pending Moderation
  1. Friday, 8 June 2018 09:42 AM UTC
  2. PowerBuilder
  3. # 2

Probably you should set a request header. Something like this:

ls_access = 'Bearer asdf1234'
ln_http.SetRequestHeader("Authorization", ls_access)

Comment
  1. Andrew Davis
  2. Friday, 8 June 2018 10:14 AM UTC
Simone



Thanks for your reply - however that is not working - i still get 401 {"error":"Unauthorized"}



regards



Andrew

  1. Helpful
  1. John Niespodzianski
  2. Tuesday, 24 July 2018 17:50 PM UTC
Are you using the restclient or the httpclient? I've successfully consumed RESTful services with both using R2.



One time saving trick I'll use is to connect to the service via SoapUI, and look at how it configures the request headers. Then I just reproduce that in my code and clean it up as needed.



A working example on my end using httpclient:



http.SecureProtocol=0 // Defaults to the server security setting

http.SetRequestHeader("Accept", "*/*")

http.SetRequestHeader("Accept-Language", "en")

http.SetRequestHeader("Connection", "close")

http.SetRequestHeader("Content-Type", "application/json")

http.SetRequestHeader("Host", ls_host)

http.SetRequestHeader("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)")

http.SetRequestHeader("Authorization", "Bearer " + ls_token)



ll_rtn = http.SendRequest('GET', ls_url)

ll_http_code = http.GetResponseStatusCode()

ls_ContentType = http.GetResponseHeader("Content-Type")

ls_AllHeaders = http.GetResponseHeaders()

http.GetResponseBody(ls_json)



  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.