SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Call SAP Conversational AI API Using OAuth

Retrieve an OAuth token and use it to call the SAP Conversational AI API, using Python to create a web service.

Overview

🎓 beginner 10 min. SAP Conversational AiBeginnerSecurityPython

You will learn

  • โœ”How to retrieve an OAuth token for use with SAP Conversational AI API
  • โœ”How to call SAP Conversational AI API via OAuth
Unknown U Unknown November 4, 2022
Created by September 22, 2021
Contributors

Prerequisites

Prerequisites

Steps

Intro

This tutorial is a companion to the tutorial Call SAP Conversational AI API Using OAuth (Postman), which lets you just test the APIs via a simple Postman collection. Here, you will make a more sophisticated simulation, including building a web server and using caching to store the OAuth token.

What you will build

This tutorial demonstrates the basics for calling the SAP Conversational AI API using an OAuth token, which is the required way for all new chatbots. The request endpoint of the Runtime API is called, which sends an utterance only to the NLP for understanding, but the principles are the same if you used the dialog endpoint.

To make things more interesting, you will create a simple web server that:

  • Takes an utterance via a URL parameter.
  • Retrieves an OAuth token, using the chatbot’s client ID and secret.
  • Calls the NLP via the request endpoint, sending the OAuth token as authentication.

Best practices call for only retrieving the OAuth once, and then caching it for all subsequent calls. We will use the flash_caching package to store the token.


Step 1 Create Python project
โ€”

  1. In the file explorer, create a new folder for the project and call it chatbot-api.

  2. Open VS Code.

    Make sure you have installed the Microsoft extension for Python, as well as all the prerequisite packages.

  3. Go to File > Add Folder to Workspace, and select the project folder.

  4. Inside the folder, create a folder called static. Download the SAP Conversational AI icon and place it in the folder.

Your project should look like this:

Project start
Project start

Step 2 Create config file and store credentials
+
Step 3 Create main Python file
+
Step 4 Add configuration to enable caching of token
+
Step 5 Add function to get OAuth token
+
Step 6 Add function to call API
+
Step 7 Test the app
+
Step 8 Test yourself
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 8
1. Create Python project 2. Create config file and store credentials 3. Create main Python file 4. Add configuration to enable caching of token 5. Add function to get OAuth token 6. Add function to call API 7. Test the app 8. Test yourself

Learn more →