Hello everyone, how are you?
So, we are facing a problem here to connect to an WebAPI that has an authentication through Bearer Token.
We use a same similar code to connect others APIs, however these ones are authenticated through Basic Auth and we are facing no issues to them.
The error that is returning to us is this:
TOKEN: <TOKEN_ID>
Expires: 06/24/2021
Status: 400
Result GET API: <html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
</body>
</html>
This is our code (replacing IDs/Secrets with <> due to security):
declare
l_result clob;
begin
apex_web_service.g_request_headers.delete();
apex_web_service.g_request_headers(1).name := 'grant_type';
apex_web_service.g_request_headers(1).value := 'client_credentials';
apex_web_service.g_request_headers(2).name := 'resource';
apex_web_service.g_request_headers(2).value := <resource_id>;
apex_web_service.oauth_authenticate(
p_token_url => <url_token>
, p_client_id => <client_id>
, p_client_secret => <client_secret>
);
apex_web_service.g_request_headers.delete();
apex_web_service.g_request_headers(1).name := 'Authorization';
apex_web_service.g_request_headers(1).value := 'Bearer ' || apex_web_service.g_oauth_token.token;
apex_web_service.g_request_headers(2).name := 'x-api-key';
apex_web_service.g_request_headers(2).value := <api_key>;
apex_web_service.g_request_headers(3).name := 'Content-Type';
apex_web_service.g_request_headers(3).value := 'application/json';
l_result := apex_web_service.make_rest_request(
p_url => <api_url>
, p_http_method => 'GET'
, p_scheme => 'OAUTH_CLIENT_CRED'
, p_parm_name => apex_util.string_to_table('cpf')
, p_parm_value => apex_util.string_to_table('999999999999')
);
DBMS_OUTPUT.PUT_LINE('TOKEN: ' || apex_web_service.g_oauth_token.token || chr(013) ||
'Expires: '|| apex_web_service.g_oauth_token.Expires || chr(013) ||
'Status: ' || apex_web_service.g_status_code || chr(13) ||
'Result GET API: '||l_result);
end;
One more info, our APEX is set at Oracle Cloud Infrastructure.
We really appreciate any help.
Thanks!
Weric Silva