ABAP SDK for Google Cloud, is a Google Cloud product that enables native bi-directional and real-time integration between SAP Applications and Google’s services. With the latest release V1.5 of the SDK, Google Workspace APIs like Gmail, Sheets, Docs, Drive and Chat are also available for SAP developers to consume natively from their SAP applications. To support Workspace APIs, a new authentication feature OAuth 2.0 with client credentials is also available in V1.5 to authenticate with the user’s own Google credentials.
Along with workspace APIs, V1.5 of the SDK now supports 40+ additional APIs in the areas of Google Vertex AI, Google Maps, and other Google Enterprise APIs (Launch Blog). Be it calling a Generative AI model from your ABAP layer, pulling in order information from Gmail or Sheets, building delivery/shipment routing use cases using Maps or redacting sensitive information from texts, the opportunities for building robust next generation SAP applications with Google’s AI/ML services are limitless.
In this blog post, I would be focusing on invoking the Gmail API to first get a list of Purchase Order request emails from a Gmail mailbox based on filters such as “Labels”, then to get contents of the in mail order requests along with attachments having detailed order items, then use Cloud Document AI to parse the attachment to get PO entities, and then finally calling SAP BAPI to create Sales Orders from the entities extracted.
Before we start, let’s look at the below prerequisites needed for invoking the API.
Google Workspace APIs use OAuth 2.0 Client Credentials based authentication, the foundation components to enable the same are embedded within the SDK. Authentication setup guidelines can be referred to setup Google Cloud console and SAP OAuth artefacts to enable the authentication for Gmail API. Below is the setup done for invoking Gmail API for this blog.
Create OAuth Client ID Credentials on Google Cloud console under the GCP project where you have enabled the Gmail API.
Create an OAuth Client profile with the provider type “/GOOG/ABAP_SDK” shipped with the SDK.
For this blog I have added all the scopes for Gmail API in the OAuth profile, you can also pick and choose the scopes from the link here for giving specific authorisation to a user for a particular set of operations.
Configure OAuth 2.0 Client profile by,
Setup OAuth 2.0 Grant by going to SAP transaction code “OA2C_GRANT”, a Web Dynpro application opens up, select the Client ID line item given in the OAuth config from before,
For invoking any Google API through ABAP SDK for Google Cloud, client key configuration is required to denote the connectivity and the authentication mechanism to be used. SPRO node
“Configure Client Key” under “Basic Settings” of node “ABAP SDK for Google Cloud” (shipped with the SDK) can be used to configure the client key table.
For this blog, we would configure the client key to invoke Gmail API as below,
You can run the “Authentication Configuration Validation Tool” shipped with the SDK from the SPRO node shipped with the SDK to validate the configuration.
An SAP ABAP automation program can be written to run as a background job to run at a frequency and leverage client library classes of latest release of ABAP SDK for Google Cloud to,
Call Gmail API to,
Methods of ABAP class “/GOOG/CL_GMAIL_V1” shipped with the SDK can be used to realize the above steps.
Call Cloud Document AI API to pass the fetched attachment data to parse and get PO entities, methods of class “/GOOG/CL_DOCUMENTAI_V1” can be used to do this.
Call SAP standard BAPI to create Sales Orders by passing the parsed PO entities.
Here is an example of a PO request from a vendor having a PDF attachment with Purchase Order details with a label “PODEMO” associated with it.
In the automation program logic,
It is also possible to list the messages by specifying more than one label ids, for example if you would like to list all the email messages which are in your “Inbox” and are “Unread”, you can introduce below code snippet in the code sample before calling method “LIST_MESSAGES”.
lo_client->set_overriding_uri(
EXPORTING
iv_uri = '/gmail/v1/users/me/messages?labelIds=UNREAD&labelIds=INBOX' ).
In a loop iteration, for each email message from the list (LS_OUTPUT-MESSAGES[ ] from above step),
For each attachment id,
Developers would have to do some post processing logic to extract the attachment content out of the response data, here is a code sample to get contents of an email attachment in Base64 encoded string.
DATA:
ls_input TYPE /goog/cl_documentai_v1=>ty_084.
TRY.
* Open HTTP Connection
DATA(lo_client) = NEW /goog/cl_documentai_v1( iv_key_name = '<client_key>' ).
* Populate relevant parameters
ls_input-raw_document-content = ls_output_attachment-data. "from the code sample above
ls_input-raw_document-mime_type = 'application/pdf'.
* Call API method: documentai.projects.locations.processors.process
CALL METHOD lo_client->process_processors
EXPORTING
iv_p_projects_id = 'GCP Project Name'
iv_p_locations_id = 'Location ID'
iv_p_processors_id = 'Processor ID of the Purchase Order Parser'
is_input = ls_input
IMPORTING
es_output = DATA(ls_output)
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(ls_err_resp).
IF lo_client->is_success( lv_ret_code ).
MESSAGE 'Success' TYPE 'S'.
ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.
* Close HTTP Connection
lo_client->close( ).
CATCH /goog/cx_sdk INTO DATA(lo_exception).
* Handle exceptions
ENDTRY.
Below is a representation of the above steps to fetch PO requests from Gmail mailbox and then use Cloud Document AI to extract PO entities from attachments to create Sales Orders, all made possible natively from your SAP automation application by ABAP SDK for Google Cloud.
Taking an example of Gmail API, the blog showed how to invoke a Google Workspace API from your SAP application using ABAP SDK for Google Cloud. We also used Cloud Document AI during the same runtime to parse and extract PO entities from Gmail attachments. With ABAP SDK for Google Cloud V1.5, we bring many more opportunities of “amalgamation” scenarios to call Google’s services to realize your complex business scenarios. With our design approach of “Single Window of Interaction”, you can quickly instantiate and invoke Google APIs at any point in your logic flow. For example, the above storyline of PO processing can further be enhanced to,
Have a look at the below video to see these in action.
Hope the blog gave you a glimpse of capabilities of ABAP SDK for Google Cloud with respect to Workspace APIs. In this blog we focussed on Gmail API and started with a few configuration steps for OAuth and Client Key setup. This remains the same for other workspace APIs too.
Bookmark What’s new with the ABAP SDK for Google Cloud for the latest announcements and follow installation and configuration instructions.
Check out these blog posts to get started with ABAP SDK for Google Cloud
The ABAP SDK for Google Cloud Community is now open! This is a place for you to ask questions, share knowledge, and collaborate with other ABAP developers who are using Google Cloud. We encourage you to get involved in the community and help us make the ABAP SDK for Google Cloud even better. We have a lot of exciting things planned for the future, and we want you to be a part of it.
Click below link to join and innovate with us.
Google Cloud community channel for ABAP SDK for Google Cloud