ABAP SDK for Google Cloud enables Google Workspace APIs to be consumed from SAP applications

deveshsapcloud_0-1698767275191.jpeg

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 begin

Before we start, let’s look at the below prerequisites needed for invoking the API.

deveshsapcloud_4-1698767857880.png

Setting up OAuth 2.0 Client

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.

Setup Client Credentials

Create OAuth Client ID Credentials on Google Cloud console under the GCP project where you have enabled the Gmail API.

deveshsapcloud_5-1698767894136.png

Setup OAuth 2.0 Client Profile for Gmail API

Create an OAuth Client profile with the provider type “/GOOG/ABAP_SDK” shipped with the SDK.

deveshsapcloud_6-1698767927114.png

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

Configure OAuth 2.0 Client profile by,

  1. Going to SAP TCode “OA2C_CONFIG”, a WebDynpro application opens up, Click on “Create”.
  2. Selecting the created “OAuth Client Profile” from the drop down and giving the “Client ID” from OAuth credentials created in Google Cloud console earlier, Click on “OK”.
  3. Giving the “Client Secret” from the OAuth credentials created in Google Cloud console earlier and clicking on “Save”.
deveshsapcloud_7-1698767965170.png

Setup OAuth 2.0 Grant

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,

  • Click on “Request OAuth 2.0 Tokens”, and sign in with your Google ID.
  • On the consent screen that opens up, click on “Allow”.
  • The WebDynpro application shows that the request is granted with the “Green” icon.
deveshsapcloud_8-1698768003936.png
Setup OAuth 2.0 Grant

Process POs from Gmail

Setup Client Key configuration

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.

deveshsapcloud_9-1698768050964.png

For this blog, we would configure the client key to invoke Gmail API as below,

  • Specify the Client Key name as “TEST_GMAIL”
  • Specify the Google Cloud project name
  • Specify the Google Cloud Service Account in email address format
  • Provide authorization class as “/GOOG/CL_OAUTH_GOOGLE”, which is shipped with the SDK to enable OAuth client credentials based authentication to Google Workspace APIs
  • Provide OAuth Client Profile prepared earlier with OAuth scopes for Gmail, this is used during authentication to derive user’s authorization to access different operations of the API
deveshsapcloud_10-1698768093939.png

You can run the “Authentication Configuration Validation Tool” shipped with the SDK from the SPRO node shipped with the SDK to validate the configuration.

deveshsapcloud_11-1698768131407.png

Solution in detail

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,

  • Poll and list the email messages with incoming PO Requests
  • Get message contents (email body and attachment details),
  • Get attachment data having PO items from the email messages.

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.

deveshsapcloud_12-1698768181324.png

List Gmail messages with PO requests

In the automation program logic,

  • Call method “LIST_MESSAGES” of class “/GOOG/CL_GMAIL_V1” to get a list of emails from the Gmail account.
  • Email ID is a mandatory parameter to be passed as input under “IV_P_USER_ID”,
  • You can hardcode “me” as string input to by default pass your email id,
  • Otherwise pass the email id of the Gmail inbox in email id format.
  • Label ID can be passed in query parameter “IV_Q_LABELIDS”, pass label id as “PODEMO”.
  • Each email message in Gmail is associated with a message id and a thread id to uniquely identify the message.
  • The method returns the messages in ABAP Type “TY_033” (ListMessagesResponse) with the unique message ids.
  • Here is a code sample to list all the emails with the “Label” in response captured in structure “LS_OUTPUT”.
  • “MESSAGES” field in the response structure holds the list of email message ids.

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' ).

Get contents of the PO request

In a loop iteration, for each email message from the list (LS_OUTPUT-MESSAGES[ ] from above step),

  • Call method “GET_MESSAGES” of class “/GOOG/CL_GMAIL_V1” get the content of an email message as a multipart output embedded in a message.
  • Email ID is a mandatory parameter to be passed as input under “IV_P_USER_ID”,
  • You can hardcode “me” as string input to by default pass your email id,
  • Otherwise pass the email id of the Gmail inbox in email id format.
  • Message ID is a mandatory parameter to be passed under “IV_P_ID” and can be passed from the loop iteration of “LS_OUTPUT-MESSAGES[n]-ID”.
  • The method returns the content in ABAP Type “TY_037” (Message) which has embedded type “TY_038” (MessagePart) to hold the message parts.
  • Each email message in Gmail is associated with a message id and a thread id to uniquely identify the message and the response .
  • Developers would have to do some post processing logic to extract the content out of the response data, here is a code sample to get contents of an email message.
  • Each attachment in a Gmail message is associated with an “Attachment ID” to uniquely identify the attachment, and the same is available in the field “ATTACHMENT_ID” of the “BODY” of the message part (Line 111 in the code sample).

Get contents of the PO attachment

For each attachment id,

  • Call method “GET_ATTACHMENTS” of class “/GOOG/CL_GMAIL_V1” to read the attachment data in a Base64 encoded string.
  • Email ID is a mandatory parameter to be passed as input under “IV_P_USER_ID”,
  • You can hardcode “me” as string input to by default pass your email id,
  • Otherwise pass the email id of the Gmail inbox in email id format.
  • Message ID is a mandatory parameter to be passed under “IV_P_MESSAGE_ID” and can be passed from the loop iteration.
  • Attachment ID is a mandatory parameter to be passed under “IV_P_ID” and can be passed from the loop iteration.
  • The method returns the attachment content in ABAP Type “TY_039” (MessagePartBody) as response under the field “DATA”.

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.

Parse contents of the PO attachment

  • The returned encoded data can be passed to Google Cloud’s Document AI using class “/GOOG/CL_DOCUMENTAI_V1” enabled in the SDK.
  • Method “PROCESS_PROCESSORS” can be used to parse the document and extract the entities, below is a code snippet as reference.
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.

Create Sales Orders from POs

  • Prepare parameters of BAPI “BAPI_SALESORDER_CREATEFROMDAT2” by referring to the extracted entities from each Purchase Order attachment from above step.
  • Call the BAPI to create Sales Orders in the loop iteration.

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.

deveshsapcloud_13-1698768358897.png

Opportunities for unlimited innovation

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,

  • After parsing the PO attachment, Address Validation API can be called using the SDK to validate and correct the Sold-to party and Ship-to party addresses before using the same to look up the SAP sold to and ship to numbers, thus reducing errors.
  • Translation AI API can be called to translate the “Delivery Instructions” from foreign language to local language making it easier for delivery personnel to understand the same.
  • Cloud Pub/Sub API can be used to publish the “Order Confirmation” message to the enterprise to trigger any follow on business process.

Have a look at the below video to see these in action.

Conclusion and next steps

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.

Ready to start using ABAP SDK for Google Cloud?

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

  • This blog, explains how you can evaluate ABAP SDK for Google Cloud using ABAP Platform Trial 1909 on Google Cloud Platform.
  • Read this blog post to get a sneak peek on how a business process such as Sales Order entry in SAP can be automated using ABAP SDK for Google Cloud with Google Cloud Storage.
  • This blog is an excellent start to understand how BigQuery ML which is a powerful machine learning service that lets you build and deploy models using SQL queries. you can now be accessed with ABAP SDK for Google Cloud.
  • Read this blog to see how "Predictive Maintenance" of equipment can be achieved in SAP using SAP and ABAP SDK for Google Cloud.
  • Read this blog post to see how you can redact sensitive information from your SAP data using Data Loss and Prevention API.
  • Read this blog post to understand how to use Secret Manager with ABAP SDK.
  • Also check out blog post about ABAP SDK Code Wizard, and on Application logging as some of the many Engineering excellence delivered as part of ABAP SDK.

Join the community today !!!!

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 

 

 

 

Contributors
Version history
Last update:
‎10-31-2023 09:59 AM
Updated by: