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!

Using Microsoft Graph API with APEX_WEB_SERVICES

melinda_fMay 12 2020 — edited May 12 2020

Hello,

I need to write an integration between our application and SharePoint using the Microsoft Graph API.  I am using APEX_WEB_SERVICES to make the calls.  I am able to successfully search and create folders, but I'm unable to upload files.  I was able to upload the files successfully in Postman, but not using APEX_WEB_SERVICES package.  I keep getting a 400 bad request error.

Below is snippet of code.  I copied the URL built in postman to verify that I had built the URL correctly and it works in Postman.  I've tried sending with and without content-length header.  I've also verified the file size is below the 4 mb limit.  I also tried converting the file blob to a base64encoded clob and sending it in the p_body parameter and no luck there either.  I suspect there is something about how the file is being sent but not sure what or how to resolve.  Does anybody have any experience calling this API from an Oracle database?

  function upload_file(p_site_id in varchar2

                       , p_folder_id in varchar2

                       , p_filename in varchar2

                       , p_file_blob in blob

                       , p_file_mimetype in varchar2) return varchar2 is

    l_url            varchar2(2000);

    l_clob           clob;

    v_count          number := 0;

    sJsonIndex       APEX_JSON.t_values;

    v_file_id        varchar2(32767);

    v_error_code     varchar2(32767);

    l_request_body_length_blob number;

  begin 

    if g_access_token is null then

      graph_api_pkg.authenticate;

    end if;

   

    l_url := 'https://graph.microsoft.com/v1.0/sites/'||p_site_id||'/drive/items/'||p_folder_id||':/'||p_filename||':/content';

    dbms_output.put_line(l_url);

  

    dbms_output.put_line('Content-Type = '||p_file_mimetype);

    apex_web_service.g_request_headers(1).name := 'Content-Type';

    apex_web_service.g_request_headers(1).value := p_file_mimetype;

    apex_web_service.g_request_headers(2).name := 'Authorization';

    apex_web_service.g_request_headers(2).value := g_token_type || ' ' || g_access_token;

  

  

    l_request_body_length_blob := dbms_lob.getlength(p_file_blob);

    dbms_output.put_line('Content-Length = '||l_request_body_length_blob);

  

    apex_web_service.g_request_headers(3).name := 'Content-Length';

    apex_web_service.g_request_headers(3).value := l_request_body_length_blob;

   

    l_clob := APEX_WEB_SERVICE.make_rest_request(

      p_url         => l_url,

      p_http_method => 'PUT',

      p_wallet_path => g_wallet_path,

      p_wallet_pwd  => g_wallet_password,

      p_body_blob   => p_file_blob

    );

    dbms_output.put_line(apex_web_service.g_status_code);

  

    for i in 1.. apex_web_service.g_headers.count loop

      dbms_output.put_line(apex_web_service.g_headers(i).name||' '||apex_web_service.g_headers(i).value);

    end loop;

  

    apex_json.parse(l_clob);

    dbms_output.put_line(dbms_lob.substr(l_clob, 4000, 1));

  

    v_file_id := apex_json.get_varchar2(p_path => 'id');

  

    if v_file_id is null then

      v_count := APEX_JSON.get_count(p_path => 'error');

    

      for i in 1 .. v_count loop

        v_error_code := apex_json.get_varchar2(p_path => 'error.code');

      end loop;

           

      return v_error_code;

    end if;

  

    return v_file_id;

  end upload_file;

This post has been answered by melinda_f on May 12 2020
Jump to Answer
Comments
Post Details
Added on May 12 2020
5 comments
3,878 views