Skip to Main Content

ORDS, SODA & JSON in the Database

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!

ORDS - How to request access token using PL/SQL

c880077e-5fca-4d0c-b87c-56fbd010cc7fMar 15 2018 — edited Mar 15 2018

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!

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 12 2018
Added on Mar 15 2018
3 comments
10,581 views