1. Daniel Ferrreira
  2. PowerBuilder
  3. Friday, 5 March 2021 14:48 PM UTC

Hi guys,

I have created a POSTMAN call to WooCommerce, to list all products, that works fine.
I'll put here the real values, as I created them only for this purpose (it's a read only key, I'll disable them as soon as I get this going).

Postman, with Basic Auth, that works (retrieves all products), with no special headers:
GET on URL https://woocriar.com/wp-json/wc/v3/products
consumer key: ck_dc94421352a19885a3fd2e313c5fb8c876b665e1
consumer secret: cs_fa07a3088bbf3b99a2eaae10b809e18e640e3087

Now I'm trying to implement the same thing on PB 2019 R2 (but I'll upgrade to R3 this weekend)

Blob lblb_data
String ls_Base64Str, ls_json, ls_url = 'https://woocriar.com/wp-json/wc/v3/products'
String ls_ck = 'ck_dc94421352a19885a3fd2e313c5fb8c876b665e1'
String ls_cp = 'cs_fa07a3088bbf3b99a2eaae10b809e18e640e3087'
Long ll_ret
RestClient restC
CoderObject lnv_CoderObject

lblb_data = Blob(ls_ck + ',' + ls_cp, EncodingANSI!)

lnv_CoderObject = Create CoderObject
ls_Base64Str = lnv_CoderObject.Base64Encode(lblb_data)

restC = Create RestClient
restC.SetRequestHeader('Authorization', 'Basic ' + ls_Base64Str)
restC.SetRequestHeader('Content-Type', 'application/json; charset=utf-8')
restC.SetRequestHeader('Connection', 'keep-alive')
restC.SetRequestHeader('Accept-Encoding', 'gzip, deflate, br')
restC.SetRequestHeader('Accept-Encoding', '*/*')

ll_ret = restC.SendGetRequest(ls_url, ls_json)

My ll_ret is returning 1, but my ls_json, instead of the products, is returning 401.
{"code":"woocommerce_rest_cannot_view","message":"Sorry, you cannot list resources.","data":{"status":401}}

Can you guys point me in the right direction as to get this working? I have many other API calls, but this is my first one using Basic Auth.

Thank you.

Accepted Answer
Daniel Ferrreira Accepted Answer Pending Moderation
  1. Friday, 5 March 2021 14:54 PM UTC
  2. PowerBuilder
  3. # Permalink

Sorry guys, I just found the answer on another thread right here, it was on HTTPClient.
It was just that I was using "," instead of ":" when concatenating key and secret.

https://www.appeon.com/developers/get-help/knowledgebase/how-use-httpclient-or-restclient-call-api-basic-authentication.html

Thanks and sorry!

Comment
  1. Armeen Mazda @Appeon
  2. Friday, 5 March 2021 15:14 PM UTC
By the way, try the REST API tester that in SnapDevelop IDE that is included with PowerBuilder. You might like it better than Postman.
  1. Helpful
  1. Daniel Ferrreira
  2. Friday, 5 March 2021 16:43 PM UTC
Thanks a lot Armeen, I didn't know it existed, so I will check it out!

Cheers.
  1. Helpful
  1. Armeen Mazda @Appeon
  2. Friday, 5 March 2021 16:45 PM UTC
Excellent... hope you like it!
  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.