Hello Experts,
I am requesting access token from an OAuth server (2-legged authorization) using PL/SQL but always getting "Invalid client credentials" error. I am sure the Client ID and Client Secret values are correct because I am able to get the token back when executing CURL command. Below is the sample code.
SET SERVEROUTPUT ON
declare
req utl_http.req;
res utl_http.resp;
token_url VARCHAR2(1024) := 'https://***/tokens';
client_id VARCHAR2(100) := 'Ka***909';
client_secret VARCHAR2(100) := 'MP***uaP';
client_credential VARCHAR2(200) := client_id || ':' || client_secret;
client_scope VARCHAR2(100) := 'ibug***public';
client_grant_type VARCHAR2(100) := 'client_credentials';
req_body VARCHAR2(1024);
value VARCHAR2(1024);
begin
req_body := 'grant_type=' || client_grant_type || '&' || 'scope=' || client_scope;
req := UTL_HTTP.begin_request (token_url, 'POST','HTTP/1.1');
UTL_HTTP.set_header(req, 'Authorization', 'Basic ' || utl_raw.cast_to_varchar2( utl_encode.base64_encode( utl_i18n.string_to_raw( client_credential ))));
UTL_HTTP.set_header(req, 'content-type', 'application/json');
UTL_HTTP.set_header(req, 'Accept-Charset', 'UTF-8');
UTL_HTTP.set_header(req, 'Connection', 'Keep-Alive');
UTL_HTTP.set_header(req, 'Content-Length', LENGTH (req_body));
UTL_HTTP.write_text (req, req_body);
res := UTL_HTTP.get_response (req);
LOOP
UTL_HTTP.READ_LINE(res, value, TRUE);
DBMS_OUTPUT.PUT_LINE(value);
END LOOP;
UTL_HTTP.end_response (res);
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
UTL_HTTP.END_RESPONSE(res);
end;
/
Executing the above code returns {"error":"invalid_client","error_description":"Invalid client credentials"}.
Can someone please tell what might be wrong? Thanks!