Skip to Main Content

APEX

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

apex_web_service.make_rest_request resulting in an error 400

Weric SilvaJun 24 2021 — edited Jun 24 2021

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

This post has been answered by Carsten Czarski-Oracle on Jun 29 2021
Jump to Answer
Comments
Post Details
Added on Jun 24 2021
18 comments
6,571 views