Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
lalitmohan
Product and Topic Expert
Product and Topic Expert

SAP Datasphere and its open data ecosystem  is the foundation for a business data fabric. It equips any organization to deliver meaningful data to every data consumer with business context and logic intact.

In addition to enabling live connections from SAP Analytics Cloud and its Microsoft Office Add-In, it's also designed to enable customers to establish connections via an OData API with any analytical frontend solution. You can connect to the OData API and consume data exposed as views or analytic models in different clients, tools, and apps that are capable of authenticating via an OAuth client and accessing an OData service.

1.png

You can read and understand more about SAP Datasphere OData API in the following links:

In this blog post, you will understand how to consume data exposed as views or analytic model to rest clients via the OData API using OAuth2.0 flow with type authorization_code.

To follow this article, You need to have a SAP BTP global account set up to be entitled to the SAP Datasphere. Please follow the excellent blog on How to create an SAP Datasphere service instance in the SAP BTP Platform.

Before you start, ensure you have set up and launched the SAP Datasphere service instance; if not, Go to your subaccount space then the services Instance page, search for "Datasphere", and click on the three dots(…) on the rightmost side with Datasphere instance and then View Dashboard. OR Look for the URL to the newly created SAP Datasphere tenant in the email.

As a pre-requisite, a basic understanding of SAP Datasphere will be good but in-case you missed out on it, you can enjoy a free self-paced learning journey Explore SAP Datasphere on SAP Learning.

Let's get Started.

Create OAuth2.0 Client in SAP Datasphere

For consuming exposed data in third-party clients, tools, and apps via OData service requires OAuth2.0 clients. Follow the steps mentioned below to create OAuth2.0 client.

1. In the side navigation area, click System -> Administration2.png

2. Under App Integration, select Add a New OAuth Client.3.png

3. In the dialog, add a Name for the OAuth client. From the Purpose list, select Interactive Usage and enter a Redirect URI to indicate where you want redirected after authorization. Select Add4.png

4. Once the OAuth Client is created, click on the edit icon to get your client ID and secret then click on Done.5.png

5. Now at last copy the OAuth2SAML Audience and your Datasphere URL.6.png

Set up a REST Client Script

Visual Studio Code (or just VSCODE) is a free source code editor developed and maintained by Microsoft. The REST Client extension allows you to send HTTP requests and view the response in VSCODE directly.

You are going the use this REST Client to query the OData API endpoints in order to understand the OAuth2.0 flow.

  1. Open Visual Studio Code and
  2. create a file with name ds-api.http Or you can give any name but file extension should be .http Or .rest. 
  3. Copy the below code to the file.

 

 

 

###
@auth-url = <enter the OAuth2SAML Audience>
@your-client_id = <enter generated OAuth Client ID>
@your-client_secret = <enter Security Secret>
@hostname = <enter SAP Datasphere Tenant URL>​

 

 

Replace the placeholder as per your OAuth Configured Client : OAuth2SAML Audience, OAuth Client ID, Security Secret and SAP Datasphere host URI you have generated in previous step.

 

Generate Code Grant token

To consume the exposed data in third-party clients requires an OAuth2.0 flow with type authorization_code. Users must manually authenticate against the configured IDP to generate the authorization code before continuing with the remaining OAuth2.0 steps. Follow the steps listed below to generate it easily.

1. Copy the below code to the file.

 

 

 

### Right click on the below URL and select Generate code snippet
### then select Shell
{{auth-url}}/oauth/authorize?response_type=code&client_id={{your-client_id}}

 

 

 

2. Right click on the below URL and select Generate code snippet.7.png

3. Select Shell then HTTPie. It will generate the Encoded URL. Copy the URL:8.png

4. On a browser, open the URL:9.png

5. As a result of your access it will redirect you to a page where you should get the code value presented in the query parameters, copy that value (e.g. lxuC1347ZLcNg3gjqFHKjAc6gT4iB0hp)10.png6. Create a variable code with the value you have copied in the last step:

 

 

 

### variable to store authorization code
@code = edF5BQ5vVsAqpkljF2tk1qqgnW4C7JRm

 

 

 

11.png

Obtain an Access Token

The Oauth2 service generates access tokens for authenticated applications. The token returned in the Oauth2 response can be used to access protected resources on OData services.

Copy below code just below the above code to Obtain a access Token and store it to a variable:

 

 

 

### Obtain an Access Token
# @name accessTokenCall
POST {{auth-url}}/oauth/token
Authorization: Basic {{your-client_id}} {{your-client_secret}}
Content-Type: application/x-www-form-urlencoded

code={{code}}
&grant_type=authorization_code

###
@accessToken = {{accessTokenCall.response.body.access_token}}

 

 

 

12.png

Retrieve Space and Asset Data 

Now you can compose your OData request based on your SAP Datasphere tenant URL in the following format: 

  • https://<tenant_url>/api/v1/dwc/<request> OR 
  • https://<tenant_url>/dwaas-core/odata/v4/<request>

and Use the catalog request to list spaces and assets that are exposed for consumption. Read more to compose your OData request. 

To list all the spaces you have access to, enter:

 

 

 

### List all the spaces
# @name listAllSpaces
GET {{hostname}}/api/v1/dwc/catalog/spaces
Authorization: Bearer {{accessToken}}
Accept: application/json

 

 

 

 To list all the assets you have access to, enter:

 

 

 

### List all the assets
# @name listAllAssets
GET {{hostname}}/api/v1/dwc/catalog/assets
Authorization: Bearer {{accessToken}}
Accept: application/json 

 

 

 

13.png

Summary

So, now you have understood how to connect to the OData API and consume data exposed as views in a rest client by accessing the OData services and authenticating them via an OAuth client.

What’s next?

In the next blog post, we will see how to consume the exposed data via OData API in custom build analytical frontend solution. 
Keep your anticipation high for the upcoming blog posts. Stay curious!

Reference & Further Reading

SAP Datasphere Features
SAP Datasphere Power Of Business Data
Start your SAP Datasphere guided experience 
SAP Datasphere: Unleash the power of business data.
What is SAP Datasphere and what benefits does it bring to your business?
Unlock the full potential of your enterprise data with SAP Datasphere Catalog

For more information and related blog posts on the topic page for SAP Datasphere. If you have questions about SAP Datasphere you can submit them in the Q&A area for SAP Datasphere in the SAP Community.

Find out how to unleash the power of your business data with SAP’s free learning content on SAP Datasphere. It’s designed to help you enrich your data projects, simplify the data landscape, and make the most out of your investment. Check out even more role-based learning resources and opportunities to get certified in one place on  SAP Learning site.

8 Comments